-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 !

1

3 Answers 3

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);
     }
Sign up to request clarification or add additional context in comments.

14 Comments

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 ?
I am not sure, but you can do something like this: code 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); } code Let me know if this works! Cheers
No this part I have already tried but only one file is uploading , i need more than one file to be uploaded.
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.
Here is the revised 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!"; } } ?>
|
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; ?>

10 Comments

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 that is because you have use a loop to get to each item.
@ Travis You check my revised code I am using loop in it.But with that its only uploading one file-
@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.
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.
|
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:

2 Comments

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.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.