Jump to content

Can't upload some files.


Ram_Saw

Recommended Posts

Hi, guys. I have some problems. I have an html code:

<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width">
    </head>
    <body>
        <form enctype="multipart/form-data" action="upload_check.php" method="post">
            <input type="hidden" name="MAX_FILE_SIZE" value="30000">
            <input type="file" min="0" max="100" name="userfile[]" multiple="true" /><br />
            <input type="submit" value="Post">
        </form>
    </body>
</html>

So to check if I received some files:

echo $_FILES['userfile']['size'][0];
echo $_FILES['userfile']['type'][0];
echo $_FILES['userfile']['size'][1];
echo $_FILES['userfile']['type'][1];
echo $_FILES['userfile']['size'][2];
echo $_FILES['userfile']['type'][2];
echo $_FILES['userfile']['size'][3];
echo $_FILES['userfile']['type'][3];

It's just for example. And what I see? I chose these first file(I attached) and the size was 0.

But! When I chose the second file, it printed: 1005image/gif

 

I don't see the difference betweet them. *.pdf files it doesn't want to receive too. But I took the system image with extension - .png one more time and PHP-script printed NOT 0 size.

So I decided that it's because of Apache settings. I changed the upload_file_size from 2Mb(it's too much for pictures anyway) to 20Mb.

It didn't help.

 

So I'm asking you for help!

Thanks!

post-166984-0-70704700-1388876256_thumb.png

post-166984-0-86444200-1388876283.gif

Link to comment
https://forums.phpfreaks.com/topic/285096-cant-upload-some-files/
Share on other sites

There is no problem with the png file. 

 

Result:

 

  Quote
Array(    [name] => Array        (            [0] => post-166984-0-70704700-1388876256.png        )    [type] => Array        (            [0] => image/png        )    [tmp_name] => Array        (            [0] => /tmp/phpS1XWUr        )    [error] => Array        (            [0] => 0        )    [size] => Array        (            [0] => 33601        ))

 

Maybe, you have something wrong in your script, but the file is fine.

Change the position of the hidden filed to be under the file in the form. Why this happens with .jpg, .png and .pdf files? I don't know the answer :confused: There is no such thing as a problem with mp3 for instance. 

<?php

if(isset($_POST['MAX_FILE_SIZE']) and $_POST['MAX_FILE_SIZE'] == 30000) {
    
  echo "<pre>".print_r($_POST,true)."</pre>";
 
  echo "<pre>".print_r($_FILES['userfile'],true).'</pre>';
}

?>
<form enctype="multipart/form-data" action="upload_check.php" method="post">

            <input type="file" min="0" max="100" name="userfile[]" multiple="true" /><br />
            <input type="hidden" name="MAX_FILE_SIZE" value="30000">
            <input type="submit" value="Post">
</form>

Result:

 

  Quote
Array(    [MAX_FILE_SIZE] => 30000)
Array(    [name] => Array        (            [0] => post-166984-0-70704700-1388876256.png        )    [type] => Array        (            [0] => image/png        )    [tmp_name] => Array        (            [0] => /tmp/phpHGR5vV        )    [error] => Array        (            [0] => 0        )    [size] => Array        (            [0] => 33601        ))

a) the hidden MAX_FILE_SIZE form field goes before any type='file' fields if you want php to use it to test the size of the uploaded files.

 

b) the size of your test file is greater than the 30000 specified in the MAX_FILE_SIZE hidden field, resulting in a upload error value of 2.

 

you must always test for errors at any step that can fail before you can use the data you expected from that step. in this case your upload is failing with a non-zero error number.

 

test if the ['error'] element isset and is equal to a zero before trying to use any of the ['size'], ['type'], ['name'], or ['tmp_name'] values.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.