note 99304 modified in features.file-upload.errors by danbrown
| From: | danbrown@php.net | Date: | Tue, 10 Aug 2010 13:54:23 +0000 |
| Subject: | note 99304 modified in features.file-upload.errors by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-171514@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.',
6=>'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);
?>
--was--
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);
?>
http://php.net/manual/en/features.file-upload.errors.php