Php file upload isset (), empty (), is_null () or === false

I posted this question before it was resolved here. Or was it? You have the same problem again.

I have a page that loads information for a product, including a bunch of gallery images in a catalog. Another page edits this information, including gallery images. If there are images in the download field that I would like to add to the script gallery (This works), if there are no images, then process other changes on the page, but do not include the script gallery.

This works to upload a single image:

if($_FILES['fileField']['tmp_name']!="") { include_once("image_script.php"); } 

I tried a bunch of different combinations to make this work for several fields for uploading files. The following are two examples:

 if(isset($_FILES['gallery']['tmp_name'])) { include_once("gallery_script.php"); } else { header("location: inventory_list.php"); } 

and

 if(!empty($_FILES['gallery']['tmp_name'])) { include_once("gallery_script.php"); } else { header("location: inventory_list.php"); } 

Every time I try to do this, it still runs the gallery of the script if there are images or no images.

Will this be the case when I have to use something other than empty() or isset() ?

Thanks for any suggestions anyone can offer.

+7
source share
3 answers

Try the following:

 if(isset($_FILES['gallery']) && count($_FILES['gallery']['error']) == 1 && $_FILES['gallery']['error'][0] > 0){ //file not selected } else if(isset($_FILES['gallery'])){ //this is just to check if isset($_FILE). Not required. //file selected } 
+8
source

It seems that $_FILES does not contain what you expect. Start by looking at what it contains:

 print_r($_FILES); exit; 

What does this show? An easy test to download a file is simple:

 if ($_FILES['your_var_name']['tmp_name']){ echo "FILE UPLOADED!"; } 

Make sure this code displays the text "FILE UPLOADED!" when downloading a file, not otherwise.

You may have included gallery_script.php in some other part of your code, and you really are not testing the section that you think. Add liberal splatter of print / echo statements and check what your variables contain.

+7
source

Or you can use this ...

HTML

 <form action="" method="post" enctype="multipart/form-data"> <input type="file" name="gallery[]" id="gallery1" /> <input type="file" name="gallery[]" id="gallery2" /> <input type="file" name="gallery[]" id="gallery3" /> <input type="submit" value="Upload" name="submit" /> </form> 

Php

 $fileUploaded = false; //You will have to check for each member in array if it was a successful file upload foreach($_FILES["gallery"]["error"] as $key => $value){ if($value == 0){ $fileUploaded = true; break; } } if($fileUploaded) echo "Include script file."; //Include script file else echo "Exclude script file."; //Exclude script file 

It seems to publish one array every time you click submit, so that you have extra validation to check for error ...

Hope this helps ...!

+1
source

All Articles