-1

I am trying to upload multiple files with single browse button, but I couldn't got success through it . I tried suggestion mentioned in similar query. Below code is only uploading one file, I need to upload more than file with same browse button.

Any idea what is wrong in the code?

<html>
<body>

<form enctype="multipart/form-data" action="uploadj.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile[]" type="file" multiple /><br />



  <p>

    <input type="submit" name="button" id="button" value="Submit">
  </p>

</form>


</html>
</body>

Php Code :


   <?php

$get_folder = $_POST['textfield'];
mkdir ("/opt/lampp/htdocs/test_upload/" . $get_folder, 0777);
echo "Analysis Directory created successfully";

$target_path = "$get_folder/";


for($i=0;$i < count($_FILES['uploadedfile']['name']);$i++){



$target_path = $target_path . basename( $_FILES['uploadedfile']['name'][$i]);


if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'][$i], $target_path))
{
    echo "The file has been uploaded";

} else

{
  echo "There was an error uploading the file, please try again!";
}


}



?>

Thanks !

Kabhir
  • 11
  • 6

3 Answers3

1

This is wrong: $_FILES['uploadedfile'][0]['name']

You should do: $_FILES['uploadedfile']['name'][0]

I have updated this code to support multiple uploads. Let me know if this works

     for($i=0;$i<count($_FILES['uploadedfile']['tmp_name']);$i++){
      $target_path = ""; //to clear the values on each loop//
      $target_path = basename( $_FILES['uploadedfile']['name'][$i]);
      move_uploaded_file($_FILES['uploadedfile']['tmp_name'][$i], $target_path);
     }
Ash
  • 27
  • 6
  • Thanks for your reply! As I tried your suggestion but by it only one file is uploading ,not more than one. Do you what can be the reason ? – Kabhir Mar 29 '16 at 08:31
  • I am not sure, but you can do something like this: `code` for($i=0;$i – Ash Mar 29 '16 at 12:32
  • No this part I have already tried but only one file is uploading , i need more than one file to be uploaded. – Kabhir Mar 29 '16 at 12:44
  • Can you update your code with what you have done so far? move_uploaded_file($_FILES['uploadedfile']['tmp_name'][$i], $target_path); If you put this on that loop it should upload multiple files because $i is changing on each iteration. – Ash Mar 29 '16 at 12:51
  • Here is the revised php code : – Kabhir Mar 29 '16 at 12:56
  • This looks too weird. Idk what you are planning to do here but with that con-cat thing the file name will be so long. Can you try this instead: $upload_folder = "$get_folder/"; $target_path = $upload_folder. basename( $_FILES['uploadedfile']['name'][$i]); – Ash Mar 29 '16 at 13:18
  • Actually I am trying to create a directory then to upload files in it. So with one file its working perfectly, but not with more than file.I tried your suggestion mentione above , but still its the same. – Kabhir Mar 29 '16 at 13:37
  • Okay, so you are uploading all the files on the folder that was just created right? And also did you try to check print_r($_FILES); and see if all the images you are uploading are coming through? – Ash Mar 29 '16 at 13:42
  • Yes I checked but only the one file is uploading. – Kabhir Mar 29 '16 at 13:48
  • Do you know what can be the problem in it ?? – Kabhir Mar 29 '16 at 14:09
  • You know with basename($_FILES['uploadedfile']['name'][$i]); it will remove all the extensions from the file? If the extensions r gone, it wont be saved as an image. It will be just be an Object. And i am not sure why is it not working because, i just did a demo code with your forms and I got : Array ( [uploadedfile] => Array ( [name] => Array ( [0] => Chrysanthemum.jpg [1] => Desert.jpg [2] => Hydrangeas.jpg ) [type] => Array ( [0] => [1] => [2] =>) [tmp_name] => Array ( [0] => [1] => [2] =>) [error] => Array ( [0] => 2 [1] => 2 [2] => 2 ) [size] => Array ( [0] => 0 [1] => 0 [2] => 0) ) ) – Ash Mar 29 '16 at 14:33
  • But the code is for text files. I am also trying to upload text files. But I getting this Analysis Directory created successfullyThe file has been uploadedArray ( [uploadedfile] => Array ( [name] => Array ( [0] => Dokument.txt ) [type] => Array ( [0] => text/plain ) [tmp_name] => Array ( [0] => /opt/lampp/temp/phpUWqhj4 ) [error] => Array ( [0] => 0 ) [size] => Array ( [0] => 4 ) ) ) – Kabhir Mar 29 '16 at 14:41
  • I used your form and then removed the action from the form, and did print_r($_FILES) and for($i=0;$i < count($_FILES['uploadedfile']['name']);$i++) { echo $target_path = basename($_FILES['uploadedfile']['name'][$i])."
    "; } Array ( [uploadedfile] => Array ( [name] => Array ( [0] => Example.odt [1] =>acb.pdf ) [type] => Array ( [0] => application/vnd.oasis.opendocument.text [1] => ) [tmp_name] => Array ( [0] => /tmp/phpTqTLD0 [1] => ) [error] => Array ( [0] => 0 [1] => 2 ) [size] => Array ( [0] => 36227 [1] => 0 ) ) ) Example.odt acb.pdf I am not sure mate! This should definitely works,
    – Ash Mar 29 '16 at 14:53
  • But without action how it recognize the php script. Can you please edited in your previous php code, b/c its showing an error. – Kabhir Mar 29 '16 at 15:12
0

One error you have is you are using $target_path before defining it. The line I am referring to is:

$target_path = $target_path . basename( $_FILES['uploadedfile'][0]['name']);

You cannot use $target_path on the right side when it has not been previously defined. On top of that the 0 is in the wrong position. It should be $_FILES['uploadedfile']['name'][0].

Other errors you may have:

  1. Not a high enough file upload size in php.ini
  2. Not a high enough max input size in php.ini
  3. Folder is not writable by the webserver user (often www-data or apache)

UPDATE

Another error, is that you are simply appending to $target_path in your loop this makes it so that the second iteration is trying to use the previous upload as a folder, which is not allowed. For an example, if I uploaded foo.png and bar.jpg the second target path would be /opt/lampp/htdocs/test_upload/foo.png/bar.png, which is not a valid path.

I would solve this in your loop by changing this line:

$target_path = $target_path . basename( $_FILES['uploadedfile']['name'][$i]);

to:

 $target_file =  $target_path . basename( $_FILES['uploadedfile']['name'][$i]);

And change:

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'][$i], $target_path))

to:

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'][$i], $target_file))

UPDATE 2

The following code works on my server (Ubuntu Server with upload folder belonging to www-data and permission of 755):

<?php if($_SERVER['REQUEST_METHOD'] != 'POST'):?>
<html>
<body>

<form enctype="multipart/form-data" action="index2.php" method="POST">
Choose a file to upload: <input name="uploadedfile[]" type="file" multiple /><br />



  <p>

    <input type="submit" name="button" id="button" value="Submit">
  </p>

</form>


</html>
</body>

<?php else: ?>

<?php

$target_path = "./videos/";


for($i=0;$i < count($_FILES['uploadedfile']['name']);$i++){



$target_file = $target_path . basename( $_FILES['uploadedfile']['name'][$i]);


if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'][$i], $target_file))
{
    echo "The file has been uploaded";

} else

{
  echo "There was an error uploading the file, please try again!";
}


}
?>

<?php endif; ?>
Travis Pessetto
  • 3,260
  • 4
  • 27
  • 55
  • I corrected the target.path by defining it previously. But the problem is that its only uploading one file, not more than one file. – Kabhir Mar 29 '16 at 08:59
  • @Kabhir that is because you have use a loop to get to each item. – Travis Pessetto Mar 29 '16 at 14:13
  • @ Travis You check my revised code I am using loop in it.But with that its only uploading one file- – Kabhir Mar 29 '16 at 14:23
  • @Kabhir you are using ```$target_path``` by appending to it in your loop. Since you used it in your previous iteration the second path would be something like '/opt/lampp/htdocs/test_upload/foo.png/bar.jpg' and since foo.png is a file not a folder it cannot move it to that location. I will update my answer. – Travis Pessetto Mar 29 '16 at 15:17
  • I am trying to upload text files .Actually I am trying to create a directory then to upload files in it. So with one file its working perfectly, but not with more than file. – Kabhir Mar 29 '16 at 15:22
  • @Kabhir It is the same as if you were uploading image files, you have an invalid path on the second one because of how you are using ```$target_file```. See my updated answer, hopefully it will help. – Travis Pessetto Mar 29 '16 at 15:25
  • I tried your suggestion but it doesn't work even after update in answer. Its still uploading only one file. – Kabhir Mar 29 '16 at 15:44
  • @Kabhir that is weird because I have tried it and it allows me to upload multiple files. You can see the example code I used in my answer. – Travis Pessetto Mar 29 '16 at 15:54
  • But what about you have not mentioned code for creating directory in it, b/c that is my requirement of code . As I mentioned above. – Kabhir Mar 29 '16 at 16:01
  • @Kabhir, it seems like you are not having a problem creating a folder from what I understand, so I have removed that piece of code. You can add that piece of code back in to create your folder. – Travis Pessetto Mar 29 '16 at 16:12
0

FORM:

<form enctype="multipart/form-data" action="" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile[]" type="file" multiple /><br />

 <p>
 <input type="submit" name="button" id="button" value="Submit">
 </p>

</form>

PHP CODE:

 if(isset($_POST['button'])){
     $upload_folder = "./textfiles/";
     for($i=0;$i<count($_FILES['uploadedfile']['tmp_name']);$i++){
          $target_path = ""; //to clear the values on each loop//
          $target_path = $upload_folder.basename( $_FILES['uploadedfile']['name'][$i]);
          if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'][$i], $target_path))
             {
             //do your redirect
             }else {
                //do your redirect
             }
   }
  }

NOTE: The php code will go on top of the form!. I have tested this code and it works good on my end:

Ash
  • 27
  • 6
  • I admire your help. Thanks a lot for your patience. But when I click on browse button (For example when I click browse button first time then the du.txt is loaded and when I click browse button second time then Document.txt is loaded. But when I am clicking on submit button then only one last file is submitted which is Document.txt and I need both files ). This is the problem .Thank you so much. – Kabhir Mar 29 '16 at 17:20
  • Ah, this might be problem. Try doing this: When you click on the browse button; press ctrl and then click on multiple files that you need to upload. – Ash Mar 29 '16 at 17:31