0

i am trying to upload file using the below method with the following code :

    echo "<form method='POST' action='uploadfile.php?action=addok' enctype='multipart/form-data'>
        <input type=\"file\" multiple=\"true\" name=\"userfile[]\"  />
            <input type='submit' value=' add '>
            </form>";

    if ($_GET['action'] == 'addok') {

      echo "\$_FILES['userfile']=".count($_FILES['userfile'])."<br />\$_FILES['userfile']['name']=".count($_FILES['userfile']['name'])."<br />\$_FILES=".count($_FILES)."<br />";
    }

it only returns 0 for all count. if i upload only 1 , it returns the count , otherwise .. nothing .. i'm trying to solve its issue since yesterday . can anyone please help out ?

7
  • do a var_dump($_FILES) instead to see what's really in there. PHP has some truly moronic ideas of how to build $_FILES when you're doing multiple same-name uploads. Commented Dec 9, 2013 at 18:40
  • I'm guessing that your returns 0 is because you're trying to upload more than one file in a single pass. If that's the case, then you'll need a foreach"it only returns 0 for all count. if i upload only 1" Commented Dec 9, 2013 at 18:41
  • i mean below :) , i wrote the first line in morning where i wrote the code back then before my question and forgot to double check it. Commented Dec 9, 2013 at 18:43
  • @fred-ii- i just tried with no luck , same issue.. Commented Dec 9, 2013 at 18:48
  • @MarcB var_dump($_FILES) result is array(0) { } still same issue.. Commented Dec 9, 2013 at 18:51

4 Answers 4

0
    if ($_GET['action'] == 'addok') {

        foreach($_FILES['userfile'] as $key=>$val){

              print_r($val);
        }
    }
Sign up to request clarification or add additional context in comments.

3 Comments

did you try this var_dump($_FILES['userfile']);
on multi upload , it returns NULL
did you selected the files and click the add button?
0

You are doing count of $ _FILES, which will always be 1:

try:

echo "<form method='POST' action='uploadfile.php?action=addok' enctype='multipart/form-data'>
            <input type=\"file\" multiple=\"true\" name=\"userfile[]\"  />
                <input type='submit' value=' add '>
                </form>";

        if ($_GET['action'] == 'addok') {
          echo "<pre>";
            print_r($_FILES);
            echo "</pre>";
          echo "\$_FILES['userfile']=".count($_FILES['userfile'])."<br />\$_FILES['userfile']['name']=".count($_FILES['userfile']['name'])."<br />\$_FILES=".count($_FILES['userfile']['name'])."<br />";
        }

print_r:

Array
(
    [userfile] => Array
        (
            [name] => Array
                (
                    [0] => file1.txt
                    [1] => file2.txt
                    [2] => file3.txt
                )

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

            [tmp_name] => Array
                (
                    [0] => C:\wamp\tmp\php1F5C.tmp
                    [1] => C:\wamp\tmp\php1F5D.tmp
                    [2] => C:\wamp\tmp\php1F5E.tmp
                )

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

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

        )

)

result:

$_FILES['userfile']=5
$_FILES['userfile']['name']=3
$_FILES=3

2 Comments

didnt work as well .. returns : Array ( ) $_FILES['userfile']=0 $_FILES['userfile']['name']=0 $_FILES=0 try it yourself : hussein.be/tst.php also on localhost not working..
if i choose 1 file it works fine , if i choose more than 1 file , $_FILES is empty ..
0

Check your php.ini, the function max_file_uploads() should have a value higher than 1

Then try this,

    <form method="post" action="uploadfile.php?action=addok" enctype="multipart/form-data">
    <input type="file" name="userfile[]" multiple>
    <input type="submit" value="UPLOAD">
    </form>

    $folder = “/home/username/www/uploading/”;
if (is_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name']))  {   
    if (move_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name'], $folder.$HTTP_POST_FILES['userfile']['name'])) {
         Echo “File uploaded”;
    } else {
         Echo “File not moved to destination folder. Check permissions”;
    };
} else {
     Echo “File is not uploaded.”;
}; 

You will need to make sure your upload folder has the correct permissions. chmod -R 777 to change permission so that php can upload to that folder.

Things to note though are that IE < 10.0 does not support multiple file uploads via the "multiple" attribute as the File API is still a working draft, http://caniuse.com/#feat=fileapi

HTML5 input type file's multiple attribute not working in IE?

7 Comments

did you check your permissions?
yes , i tried on my localhost as well . nothing .. and as i mentioned in other posts , it works with single file only.!
remove multiple from the input and try that. then add 2 more copies of the input field and see if it works with that.
have you checked your php.ini, the function max_file_uploads() should have a value higher than 1.
it is set to 20 .and code works with single upload but doesnt work with multiple..
|
0

the solution ::

it apears that the issue is not in the code as it is in browser .

it was not working in firefox, opera or chrome because of addon or other issue i think .

i did run firefox in safemode click shift + firefox icon to open in safe mode and it worked.

this is incase anyone faced same issue again .

Note : if someone have an idea how to detect this issue and warn user please be my gust :)

Comments

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.