Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5096772
  • 博文数量: 921
  • 博客积分: 16037
  • 博客等级: 上将
  • 技术积分: 8469
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-05 02:08
文章分类

全部博文(921)

文章存档

2020年(1)

2019年(3)

2018年(3)

2017年(6)

2016年(47)

2015年(72)

2014年(25)

2013年(72)

2012年(125)

2011年(182)

2010年(42)

2009年(14)

2008年(85)

2007年(89)

2006年(155)

分类:

2006-12-14 04:20:56

您可以对 input 域使用不同的 name 来上传多个文件。

PHP 支持同时上传多个文件并将它们的信息自动以数组的形式组织。要完成这项功能,您需要在 HTML 表单中对文件上传域使用类似于多下拉菜单和复选框的数组名称来提交。

注: 对多文件上传的支持是在 3.0.10 版本添加的。

例子 36-3. 上传多个文件

Send these files:


当以上表单被提交后,数组 $_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] 都将被同时设置并有效。




User Contributed Notes
上传多个文件
muellerATfmiDOTuniDASHpassauDOTde
21-Jan-2003 12:42
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 :-)
quinten+spam at andrew dot cmu dot edu
26-Jun-2002 01:30
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.";
  }
}
ken at gizzar dot com
15-May-2002 10:32
When you do a multiple file
upload via POST method, the most logical choice is to use the PHP array:




.
.
.



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
fjeanson at yahoo dot com
20-Feb-2002 08:35
by the way to grab the pointer to which name="userfile[]" points at in the html
, all you need is
$userfile[0] or $userfile[$i] at index $i.
ex to upload a file:
$userf = $userfile[0];
$fname = $HTTP_POST_FILES['userfile']['name'][0];
if($fname!=""){

$dat = date("d,m,y");
$fp = fopen ($userf, "rb");
$fileSize = filesize($userf);
$contents = fread($fp,$fileSize);
fclose($fp);
if ($fileSize<2000000){
$fp = fopen("$directory/$fname","wb");
fwrite($fp, $contents);
} else {
echo "
Your file is way to big!!!
";
}
fclose($fp);
}
?>

there u go ;)
阅读(2572) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~