/    Sign up×
Community /Pin to ProfileBookmark

$_FILES is NULL if submitted to another file

That is really bizarre. I have this simple form on a document, say index.php:

[code]
<form id=”file_upload_form” method=”post” enctype=”multipart/form-data” action=”upload.php”>
<input name=”file” id=”file” size=”27″ type=”file”><br>
<input type=”submit” name=”action” value=”Upload”><br>
</form>
[/code]

And in upload.php I simply have:

[code=php]
<?php
var_dump($_FILES);
?>
[/code]

Surprisingly I get [B]NULL[/B]

But if i perform an auto submit, that means I change the action of the form to action=””

[code]
<form id=”file_upload_form” method=”post” enctype=”multipart/form-data” [COLOR=”Blue”]action=””[/COLOR]>
[/code]

And i move the var_dump() in the same document, in the index.php, here it is: the $_FILES array appears!

How’s that? ?

I tried to see if there are some restrictions into the ini configuration, thus I wrote this into the upload.php

[code=php]
<?php
echo ‘file_uploads: ‘. ini_get(‘file_uploads’). ‘<br />’;
echo ‘upload_tmp_dir: ‘. ini_get(‘upload_tmp_dir’). ‘<br />’;
echo ‘upload_max_filesize: ‘. ini_get(‘upload_max_filesize’). ‘<br />’;
echo ‘max_file_uploads: ‘. ini_get(‘max_file_uploads’). ‘<br />’;
?>
[/code]

And the result is:

[code]
file_uploads: 1
upload_tmp_dir: C:WINDOWSTemp
upload_max_filesize: 2M
max_file_uploads:
[/code]

Which I think shows that I have permission to upload a file.

Important: the uploaded file is a small PNG file of 20 KB, thus the size can not be the problem.

Any ideas?

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@criterion9Jul 12.2011 — I found a nice comment on php.net:

Sometimes you may get empty $_FILES and empty $_POST.

If this happens, this is probably because the posted file size exceed the post_max_size php ini directive value.

You could use something like this to handle it in your form, here it is :

<?php

// The form isn't misbehavin then&#8230;

if (empty($_FILES) && empty($_POST) && isset($_SERVER['REQUEST_METHOD']) && strtolower($_SERVER['REQUEST_METHOD']) == 'post') {


$poidsMax = ini_get('post_max_size');

$oElement->addError("fileoverload", "Your feet is too big, maximum allowed size here is $poidsMax.");

}

?>
[/quote]


I didn't see post_max_size in your testing above. Does it have an appropriate value?
Copy linkTweet thisAlerts:
@criterion9Jul 12.2011 — Another thing to note is that the PHP docs specify:

<!-- The data encoding type, enctype, MUST be specified as below -->

<form enctype="multipart/form-data" action="__URL__" method="POST">

<!-- MAX_FILE_SIZE must precede the file input field -->

<input type="hidden" name="MAX_FILE_SIZE" value="30000" />

<!-- Name of input element determines name in $_FILES array -->

Send this file: <input name="userfile" type="file" />

<input type="submit" value="Send File" />

</form>
[/quote]

Though I'm not sure if adding in the max_file_size will change your results at all.
Copy linkTweet thisAlerts:
@KorauthorJul 14.2011 — My file does not exceed the size, as I told you. After all, if it might have exceeded the allowed size, why am I able to auto submit it on the same document, but I can not submit it to another document (on the same domain)? [I]"This is the question."[/I] ?
Copy linkTweet thisAlerts:
@criterion9Jul 14.2011 — My file does not exceed the size, as I told you. After all, if it might have exceeded the allowed size, why am I able to auto submit it on the same document, but I can not submit it to another document (on the same domain)? [I]"This is the question."[/I] ?[/QUOTE]

Are you submitting to different folders? Does the one that doesn't work have a .htaccess file in it with conflicting directives?
Copy linkTweet thisAlerts:
@KorauthorJul 15.2011 — Are you submitting to different folders? Does the one that doesn't work have a .htaccess file in it with conflicting directives?[/QUOTE]
I have a common .htaccess, which calls a password file, that is true. It is a temporary solution, till the login box and connection to DB will be ready. That might be the root of my problems, perhaps...
Copy linkTweet thisAlerts:
@criterion9Jul 15.2011 — I have a common .htaccess, which calls a password file, that is true. It is a temporary solution, till the login box and connection to DB will be ready. That might be the root of my problems, perhaps...[/QUOTE]

Another thing to consider as well is if the total size of POST (including all other fields in addition to the uploaded file) have exceeded the post_max_size. PHP doesn't give much in the way of help troubleshooting when that happens as you usually just get empty POST and FILES.
×

Success!

Help @Kor spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...