Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP HTML: File upload not working

Tags:

html

forms

php

I can't believe I have to ask this, but for some reason my file isn't working. It's called ajax.php (though don't mind the name), and here is the exact code:

<?php
error_reporting(-1);

print_r($_POST);
print_r($_FILES);
?>

<form action="ajax.php" method="post" enctype="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
    <input type="text" name="first" value="Bob" />
    <input type="text" name="middle" value="James" />
    <input type="text" name="last" value="Smith" />
    <input type="file" name="something" />
    <input type="submit" value="Submit" />
</form>

When I submit without attaching a file it prints data in the arrays. When I submit WITH a file no arrays populate.

What obvious thing am I missing???

Without file

Array ( [MAX_FILE_SIZE] => 30000 [first] => Bob [middle] => James [last] => Smith )
Array ( [something] => Array ( [name] => [type] => [tmp_name] => [error] => 4 [size] => 0 ) )

With File

Array ( )
Array ( )

EXPECTED with File

Array ( [MAX_FILE_SIZE] => 30000 [first] => Bob [middle] => James [last] => Smith )
Array ( [something] => Array ( [name] => sample.jpg [type] => image/jpg [tmp_name] => whatever.jpg [error] => 0 [size] => 1248 ) )

UPDATE

It appears to be working on another server, it's DEFINITELY some configuration with my WAMP, meaning my question was incorrectly asked and therefore I'm closing it. Apologies to anyone who wasted time on my stupidity.

like image 776
Bing Avatar asked Nov 01 '22 14:11

Bing


1 Answers

This appears to be a configuration problem. I'd say the post_max_size is too small. This would explain why the $_POST superglobal is empty when a file is uploaded. From the manual...

If the size of post data is greater than post_max_size, the $_POST and $_FILES superglobals are empty.

You need to set this value greater than upload_max_filesize. For example, one of my servers has...

file_uploads=On
upload_max_filesize=12M
post_max_size=20M
like image 92
Phil Avatar answered Nov 12 '22 18:11

Phil