View Full Version : $_FILES['userfile']['tmp_name'] is empty!

05-02-2009, 12:47 AM
I have had a file upload on my site for about a year, and all of a sudden it stopped working. I identified the problem is that the tmp_name is empty. I have way more than enough space on the server, so that isn't the problem.

if($_FILES['userfile']['name'] != "") {
$allowed_filetypes = array('.gif','.jpg','.jpeg','.png','.JPG','.GIF','.JPEG','.PNG');
$max_filesize = 2097152; // (currently 2.0MB).
$upload_path = './bgimages/';

$filename = $_FILES['userfile']['name'];
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);

list($width, $height, $type, $attr) = getimagesize($_FILES['userfile']['tmp_name'] );
if ($width > 1920 || $height > 1200)
$error = "Maximum width and height exceeded. The maximum size is 190x150.";

$error = "The file you attempted to upload is not allowed.";

if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
$error = "The file you attempted to upload is too large. 2MB maximum.";

$error = "You cannot upload to the specified directory, please CHMOD it to 777.";

if (empty($error)) {
$filename = $user_id . $_FILES['userfile']['name'];
if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename))
$msg = "Your file upload was successful.";
$error = "There was an error during the file upload. Please try again later or contact support.";

Running this gives me the error: Warning: getimagesize() [function.getimagesize]: Filename cannot be empty in /patchto/public_html/settings/createtheme.php on line 68

I also have the correct multipart/form-data enctype on the form.

Any ideas what would cause it to suddenly stop working? I'm thinking it has to be something with the server?

05-02-2009, 01:00 AM
What are you actually getting? Add the following debugging code -

echo "<pre>";
echo "POST:";
echo "FILES:";
echo "</pre>";

05-02-2009, 01:10 AM
[theme_name] => test
[category] => Education
[description] =>
[yt_link] =>
[logo] => images/logo.png
[searchbar] => 1
[bg_image] =>
[MAX_FILE_SIZE] => 524288
[bg_color] =>
[link_color] =>
[menu_border] =>
[menu_hover] =>
[menu_bg] =>
[privacy] => 0
[style] => 1
[CreateTheme] => Create theme
[userfile] => Array
[name] => amy.jpg
[type] =>
[tmp_name] =>
[error] => 2
[size] => 0


Well that is strange. :confused:

05-02-2009, 01:18 AM
A) Real code always checks for errors before blindly accessing data that might not exist,
B) Here the link to the documentation that tells you what that error code means - http://www.php.net/manual/en/features.file-upload.errors.php

05-02-2009, 01:23 AM
What a silly mistake on my part. I was using MAX_FILE_SIZE as a hidden input, but it was a lower value than the php check, duh!

Thanks for the help! :thumbsup: