note 99304 added to features.file-upload.errors
| From: | JeffMinermrjminerATgmailDOTcom at osu1 dot php dot net | Date: | Tue, 10 Aug 2010 08:32:17 +0000 |
| Subject: | note 99304 added to features.file-upload.errors | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-171504@lists.php.net to get a copy of this message | ||
One thing that is annoying is that the way these constant values are handled requires processing no
error with the equality, which wastes a little bit of space. Even though "no error" is 0,
which typically evaluates to "false" in an if statement, it will always evaluate to true
in this context.
So, instead of this:
-----
<?php
if($_FILES['userfile']['error']) {
// handle the error
} else {
// process
}
?>
-----
You have to do this:
-----
<?php
if($_FILES['userfile']['error']==0) {
// process
} else {
// handle the error
}
?>
-----
Also, ctype_digit fails, but is_int works. If you're wondering... no, it doesn't make any
sense.
To Schoschie:
You ask the question: Why make stuff complicated when you can make it easy? I ask the same
question since the version of the code you / Anonymous / Thalent (per danbrown) have posted is
unnecessary overhead and would result in a function call, as well as a potentially lengthy switch
statement. In a loop, that would be deadly... try this instead:
-----
<?php
$error_types = array(
1=>'The uploaded file exceeds the upload_max_filesize directive in php.ini.',
'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML
form.',
'The uploaded file was only partially uploaded.',
'No file was uploaded.',
'Missing a temporary folder.',
'Failed to write file to disk.',
'A PHP extension stopped the file upload.'
);
// Outside a loop...
if($_FILES['userfile']['error']==0) {
// process
} else {
$error_message = $error_types[$_FILES['userfile']['error']];
// do whatever with the error message
}
// In a loop...
for($x=0,$y=count($_FILES['userfile']['error']);$x<$y;++$x) {
if($_FILES['userfile']['error'][$x]==0) {
// process
} else {
$error_message = $error_types[$_FILES['userfile']['error'][$x]];
// Do whatever with the error message
}
}
// When you're done... if you aren't doing all of this in a function that's about to
end / complete all the processing and want to reclaim the memory
unset($error_types);
?>
----
Server IP: 69.147.83.197
Probable Submitter: 98.228.88.150
----
Manual Page -- http://www.php.net/manual/en/features.file-upload.errors.php
Edit -- https://master.php.net/note/edit/99304
Del: integrated -- https://master.php.net/note/delete/99304/integrated
Del: useless -- https://master.php.net/note/delete/99304/useless
Del: bad code -- https://master.php.net/note/delete/99304/bad+code
Del: spam -- https://master.php.net/note/delete/99304/spam
Del: non-english -- https://master.php.net/note/delete/99304/non-english
Del: in docs -- https://master.php.net/note/delete/99304/in+docs
Del: other reasons-- https://master.php.net/note/delete/99304
Reject -- https://master.php.net/note/reject/99304
Search -- https://master.php.net/manage/user-notes.php