user577732
user577732

Reputation: 4056

Website Multiple File Upload

Hi I'm very new to web development but have a coding background. I'm trying to create a very simple multiple file uploader for a form i'm creating however i cannot for the life of me get it to work. I followed a tutorial i found online and here is what i have so far. I am getting the hmm echo ha

HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"  dir="ltr">
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="content-type">
<title></title>
</head>
<body>
<form method="post" action="upload.php" enctype="multipart/form-data">
  <input name="filesToUpload[]" id="filesToUpload" type="file" multiple="" onChange="makeFileList();" />
<input id="saveForm" class="button_text" type="submit" name="submit" value="Submit" />
</form>
    <p>
        <strong>Files You Selected:</strong>
    </p>
    <ul id="fileList"><li>No Files Selected</li></ul>

    <script type="text/javascript">
        function makeFileList() {
            var input = document.getElementById("filesToUpload");
            var ul = document.getElementById("fileList");
            while (ul.hasChildNodes()) {
                ul.removeChild(ul.firstChild);
            }
            for (var i = 0; i < input.files.length; i++) {
                var li = document.createElement("li");
                li.innerHTML = input.files[i].name;
                ul.appendChild(li);
            }
            if(!ul.hasChildNodes()) {
                var li = document.createElement("li");
                li.innerHTML = 'No Files Selected';
                ul.appendChild(li);
            }
        }
    </script>
</body>
</html>

PHP

<?PHP
if(count($_FILES['uploads']['filesToUpload'])) {
  foreach ($_FILES['uploads']['filesToUpload'] as $file) {
    move_uploaded_file($file, "uploads/" . "sample.png" );
  }
}else{
echo "hmm";
}
?>

EDIT

Okay i updated the php file to this and originally had an echo in which echoed my files correctly however i can't get the actual upload to happen now i just want to add the file to my upload directory which i've set permissions on already to be 777 with filezilla and want to keep the same name of the file

<?PHP
if(count($_FILES['filesToUpload']['name'])) {
  foreach ($_FILES['filesToUpload']['name'] as $file) {
    move_uploaded_file($file, "uploads/" . $file );
  }
}else{
}
?>

Thank you very much for the help so far!

Upvotes: 0

Views: 1102

Answers (1)

Misha&#39;el
Misha&#39;el

Reputation: 76

Your code

    count($_FILES['uploads']['filesToUpload'])

Will always return zero, as that element does not actually exist in the $_FILES array.

It may be more helpful to "print_r()" your $_FILES array in the event of an error (in test only). Here is an example of the multi-file upload syntax.

    Array
    (
        [filesToUpload] => Array
            (
                [name] => Array
                    (
                        [0] => test.txt
                        [1] => test - Copy.txt
                    )

                [type] => Array
                    (
                        [0] => text/plain
                        [1] => text/plain
                    )

                [tmp_name] => Array
                    (
                        [0] => C:\xampp\tmp\php3770.tmp
                        [1] => C:\xampp\tmp\php3771.tmp
                    )

                [error] => Array
                    (
                        [0] => 0
                        [1] => 0
                    )

                [size] => Array
                    (
                        [0] => 0
                        [1] => 0
                    )

            )

    )

Using count($_FILES['filesToUpload']['name']) should be the fix for your script.

EDIT

With your code

    if(count($_FILES['filesToUpload']['name'])) {
      foreach ($_FILES['filesToUpload']['name'] as $file) {
        move_uploaded_file($file, "uploads/" . $file );
      }
    }

You are sending the name (['name'] (which is an array element) as $file), which doesn't translate, as the move_uploaded_file() is looking for the tmpname (or filepath) of the uploaded file. You can change what you have to this.

    if(count($_FILES['filesToUpload']['name'])) {
      foreach ($_FILES['filesToUpload']['tmp_name'] as $ref => $fullfilename) {
        move_uploaded_file($fullfilename, "uploads/" . $_FILES['filesToUpload']['name'][$ref] );
      }
    }

This will get you in the ballpark, but I have to mention, that there are much better ways of handling this. I will try and post a class I have for multi-file upload (or find one on Google) and link it to comment on this page. But the above, should get you (at a bare minimum) what you are looking for.

Upvotes: 3

Related Questions