Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7506361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:14:08+00:00 2026-05-29T22:14:08+00:00

I have a form to upload image like below <form name=sample enctype=multipart/form-data action=index.php method=post

  • 0

I have a form to upload image like below

<form  name="sample" enctype="multipart/form-data" action="index.php" method="post" >
<input type="hidden" name="MAX_FILE_SIZE" value="2048000" />
Select an Image: <input name="photo" id="photo" type="file" /> <input type="submit" value="Upload" />
</form>

Now i have to check something like below ..

If(any file/image selected){
i will check here whether the selected file is a valid image.
}

I have managed the validation part for if but what i can not check is whether any file is selected or not.
I used the following condition

if(!empty($_FILES))

But this execute in all cased whether someone select a file or not.

Please someone help me how can i make this work.

Here is my full PHP code:

<?php
// Do not show notice errors
error_reporting (E_ALL ^ E_NOTICE);


if(!empty($_FILES)) // Has the image been uploaded?
{
include 'config.php';

$file = $_FILES['image_file'];

$file_name = $file['name'];

$error = ''; // Empty

// Get File Extension (if any)

$ext = strtolower(substr(strrchr($file_name, "."), 1));

// Check for a correct extension. The image file hasn't an extension? Add one

   if($validation_type == 1)
   {
   $file_info = getimagesize($_FILES['image_file']['tmp_name']);

      if(empty($file_info)) // No Image?
      {
      $error .= "The uploaded file doesn't seem to be an image.";
      }
      else // An Image?
      {
      $file_mime = $file_info['mime'];

         if($ext == 'jpc' || $ext == 'jpx' || $ext == 'jb2')
         {
         $extension = $ext;
         }
         else
         {
         $extension = ($mime[$file_mime] == 'jpeg') ? 'jpg' : $mime[$file_mime];
         }

         if(!$extension)
         {
         $extension = '';  
         $file_name = str_replace('.', '', $file_name); 
         }
      }
   }
   else if($validation_type == 2)
   {
      if(!in_array($ext, $image_extensions_allowed))
      {
      $exts = implode(', ',$image_extensions_allowed);
      $error .= "You must upload a file with one of the following extensions: ".$exts;
      }

      $extension = $ext;
   }

   if($error == "") // No errors were found?
   {
   $new_file_name = strtolower($file_name);
   $new_file_name = str_replace(' ', '-', $new_file_name);
   $new_file_name = substr($new_file_name, 0, -strlen($ext));
   $new_file_name .= $extension;

   // File Name
   $move_file = move_uploaded_file($file['tmp_name'], $upload_image_to_folder.$new_file_name);

   if($move_file)
       {
       $done = 'The image has been uploaded.';
       }
   }
   else
   {
   @unlink($file['tmp_name']);
   }

   $file_uploaded = true;
}

?>

Please check the code now. I just need to change the line

if(!empty($_FILES)) 

such that if someone select any file then if condition will be true but if they don’t select any file i.e if they keep the field blank then if condition will be false

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T22:14:11+00:00Added an answer on May 29, 2026 at 10:14 pm

    You could also check like:

    
    if(isset($_FILES['photo']) && !empty($_FILES['photo']['name'])) { 
    ....
    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an upload box... <form action=upload_file.php method=post enctype=multipart/form-data><BR> <label for=file>Filename:</label><BR> <input type=file name=file
I have a basic upload form: <form method=post action= enctype=multipart/form-data > <input type=file name=logo>
I have a HTMl form <form action= method=post enctype=multipart/form-data> <p> Banner Ad Name :<br
<html> <body> <form name=Upload enctype=multipart/form-data method=post> Filename: <INPUT type=file id=submit> <INPUT type=button id=send value=Upload>
I have a raw form-data that look like this: ------------V2ymHFg03ehbqgZCaKO6jy Content-Disposition: form-data; name=intro O
I have an image upload form and at the bottom, I'd like to have
I have a form in my template like this : <form action=. method=GET style=font-family:
Hi I have a form class which looks like below:- class UserCreateForm(wtf.Form): name=wtf.TextField('Name',validators=[validators.Required(),username_check]) email=wtf.TextField('Email')
I have a method set up that uses jquery form for a file upload
I have a form that, among other things, accepts an image for upload and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.