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 ?

Hussein
  • 97
  • 12
  • 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. – Marc B Dec 09 '13 at 18:40
  • What is the “above method?” – Giacomo1968 Dec 09 '13 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"* – Funk Forty Niner Dec 09 '13 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. – Hussein Dec 09 '13 at 18:43
  • @fred-ii- i just tried with no luck , same issue.. – Hussein Dec 09 '13 at 18:48
  • @MarcB `var_dump($_FILES)` result is `array(0) { }` still same issue.. – Hussein Dec 09 '13 at 18:51
  • then no upload is being performed at all. not even a failing one. – Marc B Dec 09 '13 at 18:53
  • if i try with one file , it works fine .. : `array(1) { ["userfile"]=> array(5) { ["name"]=> array(1) { [0]=> string(7) "art.jpg" } ["type"]=> array(1) { [0]=> string(10) "image/jpeg" } ["tmp_name"]=> array(1) { [0]=> string(14) "/tmp/phpvzNbRD" } ["error"]=> array(1) { [0]=> int(0) } ["size"]=> array(1) { [0]=> int(43983) } } }` – Hussein Dec 09 '13 at 18:55

4 Answers4

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

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

              print_r($val);
        }
    }
Krish R
  • 22,583
  • 7
  • 50
  • 59
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?

Community
  • 1
  • 1
devfunkd
  • 3,164
  • 12
  • 45
  • 73
  • did you check your permissions? – devfunkd Dec 09 '13 at 19:42
  • yes , i tried on my localhost as well . nothing .. and as i mentioned in other posts , it works with single file only.! – Hussein Dec 09 '13 at 19:43
  • remove multiple from the input and try that. then add 2 more copies of the input field and see if it works with that. – devfunkd Dec 09 '13 at 19:44
  • have you checked your php.ini, the function max_file_uploads() should have a value higher than 1. – devfunkd Dec 09 '13 at 19:47
  • it is set to 20 .and code works with single upload but doesnt work with multiple.. – Hussein Dec 09 '13 at 19:52
  • try using 2 input fields instead of multiple, I suspect the issue may be with the multiple attribute. Also try a different browser as older versions of IE do not work with this attribute. – devfunkd Dec 09 '13 at 19:59
  • i did 15 min ago , it WORKS with ie only .. not firefox or opera ,i need the multiple not the regular method .i am searching for a solution as well for that and if i find i will post it here .. and if you have please assist :) – Hussein Dec 09 '13 at 20:11
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
abfurlan
  • 415
  • 5
  • 14
  • didnt work as well .. returns : `Array ( ) $_FILES['userfile']=0 $_FILES['userfile']['name']=0 $_FILES=0` try it yourself : http://hussein.be/tst.php also on localhost not working.. – Hussein Dec 09 '13 at 19:32
  • if i choose 1 file it works fine , if i choose more than 1 file , $_FILES is empty .. – Hussein Dec 09 '13 at 19:56
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 :)

Hussein
  • 97
  • 12