if your HTML is right but the $_FILES / $HTTP_POST_FILES array is empty make sure your < form > tag includes enctype="multipart/form-data"
cost me one hour to notice that. i hope i can save somebody valuable time by pointing out the obvious :-)
全部博文(921)
分类:
2006-12-14 04:20:56
您可以对 input 域使用不同的 name 来上传多个文件。
PHP 支持同时上传多个文件并将它们的信息自动以数组的形式组织。要完成这项功能,您需要在 HTML 表单中对文件上传域使用类似于多下拉菜单和复选框的数组名称来提交。
注: 对多文件上传的支持是在 3.0.10 版本添加的。
例子 36-3. 上传多个文件 |
当以上表单被提交后,数组 $_FILES['userfile']、$_FILES['userfile']['name'] 和 $_FILES['userfile']['size'] 将被初始化(在 PHP 4.1.0 以前版本是 $HTTP_POST_FILES。)如果 register_globals 的设置为 on,则和文件上传相关的全局变量也将被初始化。所有这些提交的信息都将被储存到以数字为索引的数组中。
例如,假设名为 /home/test/review.html 和 /home/test/xwp.out 的文件被提交,则 $_FILES['userfile']['name'][0] 将包含文件 review.html 的名称,而 $_FILES['userfile']['name'][1] 则将包含文件 xwp.out 的名称。类似的,$_FILES['userfile']['size'][0] 将包含文件 review.html 的大小,依此类推。
$_FILES['userfile']['name'][0]、$_FILES['userfile']['tmp_name'][0]、$_FILES['userfile']['size'][0] 以及 $_FILES['userfile']['type'][0] 都将被同时设置并有效。
if your HTML is right but the $_FILES / $HTTP_POST_FILES array is empty make sure your < form > tag includes enctype="multipart/form-data"
cost me one hour to notice that. i hope i can save somebody valuable time by pointing out the obvious :-)
Here's working file upload code snippet that works for any number of files "out of the box." It assumes that each input of type file is named 'files[]', although this could be modified so that it just iterated through the array $HTTP_POST_FILES. It will not overwrite existing files. It could be improved with more checks (ie, mime types) but this does what I need quite admirably. Let me know if you find an inefficiency.
$path_to_file = '/var/www-uploads/';
$files = $HTTP_POST_FILES['files'];
if (!ereg("/$", $path_to_file))
$path_to_file = $path_to_file."/";
foreach ($files['name'] as $key=>$name) {
if ($files['size'][$key]) {
// clean up file name
$name = ereg_replace("[^a-z0-9._]", "",
str_replace(" ", "_",
str_replace("%20", "_", strtolower($name)
)
)
);
$location = $path_to_file.$name;
while (file_exists($location))
$location .= ".copy";
copy($files['tmp_name'][$key],$location);
unlink($files['tmp_name'][$key]);
echo "\n
Successfully uploaded file: $name.";
}
}
When you do a multiple file
You would get a few arrays if you had your register globals turned on:
$uploads - an array of the tmp filenames & paths
$uploads_name - an array of the name given by the browser
$uploads_size - an array of the sizes
$uploads_type - an array of the MIME types of the file uploaded
So, if I looked at the content of $uploads_name["first"] in 'something.php' (in this case), it will give me the browser assigned name for the first file .
Next, you should look at how PHP limits file sizes in php.ini!
Have fun!
-Ken
by the way to grab the pointer to which name="userfile[]" points at in the html