0

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 ;)

Bjorn
  • 285
  • 3
  • 6
  • 19
  • I don't know if this is your problem, but you have a typo in your code: `if (count($_FILES["gallery"]["name"] > 0)){` should be `if (count($_FILES["gallery"]["name"]) > 0){` (parenthesis in the wrong place) – Robin Winslow Sep 03 '12 at 07:32
  • @ Robin: Ahh, well spotted but no didn't fix the issue. Thanks. – Bjorn Sep 03 '12 at 07:35
  • No, no luck so far. I'll be leaving this one until tomorrow. It seems no matter how I try to bypass the script when no files are uploaded it still runs the script. Weird! Then again I'm no PHP guru, it's probably something obvious that I'm over looking. – Bjorn Sep 03 '12 at 12:44
  • It would appear that no one has any ideas :( – Bjorn Sep 03 '12 at 22:52
  • I really need to get this working so if anyone has any ideas please let me know. Cheers – Bjorn Sep 05 '12 at 05:07
  • Tried this too `if ($_FILES['gallery']['name'] = 0) { header("location: inventory_list_sales.php"); } else { include_once("gallery_edit_script.php"); }` Still runs the damn gallery script even when there are no files. also this `if ($_FILES['gallery']['name'] >= 1) { include_once("gallery_edit_script.php"); } else { header("location: inventory_list_sales.php"); }` – Bjorn Sep 12 '12 at 02:37

3 Answers3

0

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!

Community
  • 1
  • 1
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
  • Thanks Praveen, yeah all of that is correct, the form works, it's the php I'm trying to figure out... HTML is now posted in the original post ;) – Bjorn Sep 03 '12 at 07:39
0

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);
     }
    
Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107
0

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 :)

Community
  • 1
  • 1
Bjorn
  • 285
  • 3
  • 6
  • 19