Question

now first thing is I've seen everywhere over the net what to do in the case that what I'm trying to do doesn't work, I've tried all of the solutions and they don't work, I'm obviously missing something.

I'm uploading multiple files from a form field. This works perfectly and runs some code that resizes etc deletes tmp files blah blah.

The problem is if I don't want to upload any files the upload and image processing script still runs throwing a bunch of errors.

I've tried the following... plus a bunch more with some weird variations :P

if($_FILES['gallery']['name']!=""){      // if files then...
include_once("gallery_edit_script.php");
  }

and

if (count($_FILES["gallery"]["name"] > 0)){   // if files count is more than 0 then...
include_once("gallery_edit_script.php");
  }

Would the fact that the gallery_edit_script.php is an include have something to do with it?

I checked the file error with...

$_FILES["gallery"]["error"]

It showed no files were selected to upload which was exactly what I wanted.

Any ideas people?

Thanks for anyone who has a look at this.

Cheers

Added HTML but like I said upload is working fine, it's when I want to post the form and not include files to upload that I want it to skip the gallery script. This is on an edit page, so the user has submitted form, data added to db and files uploaded, then comes back and wants to edit data but not upload files.

HTML (simplified as there are heaps of fields etc)

<form action="inventory_edit_lease.php" enctype="multipart/form-data" name="myForm" id="myForm" method="post">
Gallery Photos <input class="input-file" type="file" name="gallery[]" id="gallery" multiple="multiple" />
<input class="button-edititem" type="submit" name="submit" id="button" value="" onclick="javascript:return validateMyForm();"/>
</form>

Sorry I didn't add HTML first time round, form works so didn't think I really needed it ;)

Was it helpful?

Solution 3

For those interested this is what worked :)

I got this from another thread if($_FILES['files']['name']!="") run if files, don't run if no files headache

if(!empty($_FILES['gallery']['tmp_name']))
 {
 include_once("gallery_edit_script.php");
 }
   else
   {
   header("Location: inventory_list_sales.php");
   }

Funny thing is I tried this with another site I'm working on with almost identical code as I copied all the files and only edited small parts and it doesn't work lol

Thanks for everyone's help :)

OTHER TIPS

Few check lists...

  1. Make sure you have named your <input type="file" /> as gallery:

    <input type="file" />

  2. Make sure the <form> tag has a method="post" and action="" to the correct URL.

  3. Also, make sure your <form> tag has enctype="multipart/form-data" else you won't be able to upload files via that form!

We need to see the HTML Code of your file before we can suggest something. Make sure you have followed the above checklists and even then if it isn't working, post the code and let us know!

Without HTML form I'm just guessing:

  • for multiple file uploads with same name you must have the filed as
  • on server side you will receive them as $_FILES["gallery"]

    $_FILES["gallery"] will be an array of elements, eg:

     foreach($_FILES["gallery"] as $file){
         var_export($file);
     }
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top