Uploading PHP files

I have a very strange problem with a PHP script.

I am uploading a couple of files and PHP puts them all in one folder. I'm having problems sending random files and random files not being sent. So I debugged it and I got a very strange result from the $ _FILES [] array.

I tried this with three files.

$_FILES["addFile"]["name"]

Saves the names of three files.

You would expect it $_FILES["addFile"]["tmp_name"]

to contain 3 temporary names that PHP uses to copy files, but this is not the case. It contains only one name. The other 2 are empty lines that generate an error on load (which I suppress from displaying)

It is very strange. I've tried multi-pronged situations and it just keeps going. It must be something in my settings, or maybe even in 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 tmp_name is empty, the if value will be false, so it will skip the die () function.

Does anyone know what could be causing this?

Additional information: I am using Windows XP running WAMP server. This problem has never occurred before and I can access all the maps I tried to load from. Security settings for windows may not be an issue I guess.

0


source to share


3 answers


Relevant, but probably won't help: but move_uploaded_file is (slightly) a better way to handle uploaded files than copying.

Are any files large? PHP has file size limits and the time it can take to load them ...



Better to send you here than trying to write what he says:

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

+1


source


Sorry, but it seems to me that you are trying to load all 3 files with the same variable name? It is right? But that won't work because they will overwrite each other. I think the 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++;
   }

      

+3


source


Your loop logic is wrong. You use a foreach loop directly in the filename of the input file, which stores a few properties you are not interested in ("type", "size", etc.).

You should get the number of files from the first file and use that as the length of the loop:

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 still have problems, it should provide all the information you need to debug it.

+1


source







All Articles