views:

547

answers:

3

I'm having an extremely weird problem with a PHP script of mine.

I'm uploading a couple of files and having PHP put them all in one folder. I've have trouble with random files being sent and random ones not being sent. So I debugged it and I got a very weird result from the $_FILES[] array.

I tried it with 3 files.

$_FILES["addFile"]["name"] Holds the names of the 3 files.

You'd expect $_FILES["addFile"]["tmp_name"] to hold the 3 temporary names that PHP uses to copy the files, but it doesn't. It holds just one name. The other 2 are empty strings, which generate an error whilst uploading(which I supress from being displayed)

This is very odd. I've tried mulitple situations and it just keeps on happening. This must be something in my settings or perhaps even my code.

Here's my code:

$i = 0;
  if (!empty($_FILES['addFile'])) {
    foreach($_FILES['addFile'] as $addFile) {
      $fileToCopy = $_FILES["addFile"]["tmp_name"][$i];
      $fileName   = $_FILES["addFile"]["name"][$i];
      $i++;
      if(!empty($fileToCopy)){
       $copyTo = $baseDir."/".$fileName;
       @copy($fileToCopy, $copyTo) or die("cannot copy ".$fileToCopy." to ".$copyTo);
      }
     }
          exit(0);
   }

Since the tmp_name is empty, the if-value will be false so it's gonna skip the die() function.

Does anybody know what might be causing this?

further info: I'm using Windows XP, running WAMP server. Never had this problem before and I can acces all maps from which I've tried to upload. Security settings of windows can't be the issue I think.

+1  A: 

Relevent, but probably not going to help: but move_uploaded_file is a (slightly) better way to handle uploaded files than copy.

Are any of the files large? PHP has limits on the filesize and the time it can take to upload them ...

Better to send you here than attempt to write up what it says:

http://uk3.php.net/manual/en/features.file-upload.common-pitfalls.php

benlumley
It was indeed the filesize limit that did it to me.Cursus, I'll have to look up how to overcome this.
Vordreller
I mean "curses"
Vordreller
You have to edit the php.ini file and change it.
benlumley
+1  A: 

Your loop logic is incorrect. You are using a foreach loop on the file input name directly, which stores several properties that are of no interest to you ('type','size', etc).

You should get the file count from the first file and use it as the loop length:

if (!empty($_FILES['addFile']) && is_array($_FILES['addFile']['name'])) {
    $length = count($_FILES['addFile']['name']);
    for($i = 0; $i < $length; $i++) {
        $result = move_uploaded_file($_FILES['addFile']['tmp_name'][$i],$baseDir."/" . $_FILES['addFile']['name'][$i]);
         if($result === false) {
            echo 'File upload failed. The following error has occurred: ' . $_FILES['addFile']['error'][$i];
         }
     }
}

Check the error code if you are still having problems, it should provide all the information you need to debug it.

Eran Galperin
+2  A: 

I'm sorry but it seams to me that you are trying to upload all 3 files with the same variable name? Is this right? But this will not work because they will overwrite each other. I think the better an cleaner way it would be to use something like

    $i = 0;
    foreach($_FILES['addFile'.$i] as $addFile) {
     if(!empty($addFiles) {
        move_uploaded_file($addFile['temp_name'], 'YOUR DIRECTORY');
     }
      $i++;
   }
dragonlord21
He can upload them using an array variable name (addFile[])
Eran Galperin