0

i am trying to upload multiple files to a directory. the code i have "written" works for one file, however when i try to upload multiple files it does not work. i have tried to determine where the fault is and i believe it has something to do with count. although when i try to echo how many files are being counted i always get '1' regardless of how many files i have selected. i know this will only work for one file as the variable i am returning is always '1', and therefore will only work for one file.

to gather the files i am using html form with post method html:

<form id="upload" action="upload.php" method="POST" enctype="multipart/form-data">

<fieldset>
<legend>HTML File Upload</legend>

<input type="hidden" id="MAX_FILE_SIZE" name="MAX_FILE_SIZE" value="300000" />

<div>
    <label for="fileselect">Files to upload:</label>
    <input type="file" id="fileselect" name="fileselect[]" multiple="multiple" />
    <div id="filedrag">or drop files here</div>
</div>

<div id="submit">
    <button type="submit">Upload Files</button>
</div>

</fieldset>

</form>

the javascript i am using enables drag and drop feature and was sourced from: http://www.sitepoint.com/html5-file-drag-and-drop/

the culprit PHP:

if(isset($_FILES['fileselect']['tmp_name'])) {
    // Number of uploaded files
    $num_files = count($_FILES['fileselect']['tmp_name']);
    echo $num_files;
    /** loop through the array of files ***/
    for($i=0; $i <= $num_files;$i++) {
        // check if there is a file in the array
        if(!is_uploaded_file($_FILES['fileselect']['tmp_name'][$i])) {
            $messages[] = 'No file uploaded';
        }
        else {
            $unique = substr(number_format(time() * rand(),0,'',''),0,10);

            $newImg = "img".$unique;

            $filename  = basename($_FILES['file']['name'][$i]);
            $extension = pathinfo($filename, PATHINFO_EXTENSION);
            //$new       = md5($filename).'.'.$extension;

            $the_file_type = $_FILES['fileselect']['type'][$i];
            $the_file_size = $_FILES['fileselect']['size'][$i] / 1024;
            $the_file_name = $_FILES['fileselect']['name'][$i];

            $ok=1;
            //This is our size condition 
            if ($uploaded_size > 350000) { 
                $messages[] =  "Your file is too large.<br>"; 
                $ok=0; 
            } 

            //This is our limit file type condition 
            if ($uploaded_type =="text/php") { 
                $messages[] =  "No PHP files<br>"; 
                $ok=0; 
            } 

            //Here we check that $ok was not set to 0 by an error 
            if ($ok==0) { 
                $messages[] =  "Sorry your file was not uploaded"; 
            } 

            //If everything is ok we try to upload it 
            else { 
                if(move_uploaded_file($_FILES['fileselect']['tmp_name'][$i], "uploads/{$newImg}")) { 
                    echo "The file ". basename( $_FILES['uploadedfile']['name'][$i]). " has been uploaded";
                    echo "<br />";
                    //echo $the_file_type;
                    //echo "<br />";
                    //echo $unique;
                    //echo "<span>schmeckle!</span>";
                    //echo "<br />";
                    //echo $the_file_size;
                    //echo "<br />";
                    //echo $the_file_name;

                    $insertSQL = "INSERT INTO interviews_media_images SET ";
                        $insertSQL .= "fileType='$the_file_type', ";
                        $insertSQL .= "fileRef='$newImg', ";
                        $insertSQL .= "fileSize='$the_file_size', ";
                        $insertSQL .= "fileName='$the_file_name' ";

                    echo $insertSQL;

                    //mysql_query( $insertSQL);

                    //echo mysql_error();

                } else { 
                    $messages[] = "Sorry, there was a problem uploading your file."; 
                } 
            }
        }
    }
}
6
  • 1
    Did you even print_r($_FILES) to see what it actually contains? I believe it's $_FILES['fileselect'][$i]['attribute'] not $_FILES['fileselect']['attribute'][$i] Commented Jun 27, 2013 at 22:07
  • everything above: "for($i=0; $i <= $num_files;$i++){... blah}else{" , was referenced from another post on stack, which apparently works. link: stackoverflow.com/questions/2233816/… everything below works. my problem is that "$num_files = count($_FILES['fileselect']['tmp_name']);" always returns '1'. Commented Jun 27, 2013 at 22:24
  • My first guess is that there is NOT a bug in the PHP count() function Commented Jun 27, 2013 at 22:27
  • Try doing it this way: $_FILES['fileselect'][$i]['tmp_name'] or count($_FILES['fileselect']) Commented Jun 27, 2013 at 22:28
  • thanks guys. although taking out ['tmp_name'] from "$num_files = count($_FILES['fileselect']['tmp_name']);" now gives 5, and still only adds the first entry to the directory. Commented Jun 27, 2013 at 22:46

2 Answers 2

0

Your code is running in my case and $num_files returns true value after count.

One more thing to do that remove '=' from for loop and write like this

for( $i=0; $i<$num_files; $i++)

if you use $i <= $num_files it gives you a notice.

Suppose you are selecting 3 files and you are setting $i <= $num_files.

So for loop starts from 0 and as you selected 3 files, it continue for 4 times

0 1 2 3

You can see that the loop is continued for 4 times but it has no file on

$_FILES['filename']['tmp_name'][3].

I hope this will help you...

Sorry for my english.

Sign up to request clarification or add additional context in comments.

Comments

0

I created a file with the contents as follows, running PHP version 5.5.15 and it counts correctly:

<?php
if(isset($_FILES['fileselect']['tmp_name']))
{
    echo "<pre>"; print_r($_FILES); echo "</pre>";
    echo "Number of files uploaded: " . count($_FILES['fileselect']['name']);
}
?>

<form id="upload" action="files_upload.php" method="POST" enctype="multipart/form-data">

<fieldset>
<legend>HTML File Upload</legend>

<input type="hidden" id="MAX_FILE_SIZE" name="MAX_FILE_SIZE" value="300000" />

<div>
    <label for="fileselect">Files to upload:</label>
    <input type="file" id="fileselect" name="fileselect[]" multiple="multiple" />
    <div id="filedrag">or drop files here</div>
</div>

<div id="submit">
    <button type="submit">Upload Files</button>
</div>

</fieldset>

</form>

1 Comment

you need to better explain your answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.