Why wont my PHPmailer script work to get / save files?-Collection of common programming errors
First warning: You’re chmodding the uploads
directory itself. Unless the directory is owned by your webserver’s user ID, you’ll get the permission denied error – you cannot chmod something that does not belong to you.
Second and Third warning: Your array keys in the $_FILES
array are incorrect. They should be
while(list($key,$value) = each($_FILES['images']['name']))
note the quotes – without the quotes, PHP assumes they’re constants that were created via define()
. If there’s no constant by that name, PHP polite will treat them as strings of the same name, but issues the warning.
Fourth warning: You’re misusing each()
. Instead, just have:
foreach($_FILES['images']['name'] as $key => $value) {
Fifth warning: The only place you assign a value to $success
is within the if()
that does the image copy, when the file copy succeeds. Since your code is broken, the copy never takes place, so $success
is never defined. To fix, put a
$success = false;
somewhere at the top of your file, so it’s defined with a default value.
Beyond that, don’t use copy()
on uploaded files. There’s security issues involved with PHP and file uploads on shared servers. Use move_uploaded_file()
instead. This will be a far cheaper operation as well, since moving a file within a single filesystem is near instantaneous, while copy()
actually duplicates the file – on large files this gets expensive (time + cpu + disk space) very quickly.
comment followup:
It does matter how you name the input fields, as that’ll determine how things show up in the files array.
If you go with the file1
, file2
, etc… option, you end up with:
$_FILES = array(
'file1' => array('name' => ..., 'size' => ..., etc...),
'file2' => array('name' => ..., 'size' => ..., etc...),
etc...
)
If you do files[]
, you end up with:
$_FILES = array (
'files' => array
'name' => array(
0 = 'name of first file',
1 = 'name of second file',
...
'size' => array(
0 = 'size of first file'
1 = 'name of second file'
etc...
A major structural difference. I have no idea why such a moronic difference was allowed to go into production, but since this is core PHP functionality, changing it to be more logically consistent is basically impossible.