关闭

多文件上传

851人阅读 评论(0) 收藏 举报
CODE
<?php
// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
// of $_FILES.

$uploaddir = './pic/';
$MAX_SIZE = 5000000;
$FILE_MIMES = array('image/jpeg','image/jpg','image/gif'
,'image/png','application/msword');

$FILE_EXTS = array('.zip','.jpg','.png','.gif','.rar');

$DELETABLE = true;

//下一部就是设置浏览器访问变量及目录访问变量:
$site_name = $_SERVER['HTTP_HOST'];
$url_dir = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
$url_this = "http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];

$upload_dir = "files/";
//$upload_url = $url_dir./files/;
$message ="";

/*
print "<pre>";


if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
   print "File is valid, and was successfully uploaded. ";
   print "Here's some more debugging info:/n";
   print_r($_FILES);
} else {
   print "Possible file upload attack!  Here's some debugging info:/n";
   print_r($_FILES);
}


print "</pre>";
*/
?>


<?php

function reArrayFiles(&$file_post) {

  $file_ary = array();
  $file_count = count($file_post['name']);
  $file_keys = array_keys($file_post);

  for ($i=0; $i<$file_count; $i++) {
      foreach ($file_keys as $key) {
          $file_ary[$i][$key] = $file_post[$key][$i];
      }
  }

  return $file_ary;
}


print "<pre>";

if ($_FILES['userfile']) {
  $file_ary = reArrayFiles($_FILES['userfile']);

  foreach ($file_ary as $file) {
/*       print 'File Name: ' . $file['name']."<br>";
      print 'File Type: ' . $file['type']."<br>";
      print 'File Size: ' . $file['size']."<br>";
*/
    $uploadfile = $uploaddir . $file['name'];

$file_type = $file['type'];
$file_name = $file['name'];
$file_ext = strtolower(substr($file_name,strrpos($file_name,".")));

if($file['size']>$MAX_SIZE){
$message =" The file size is over 5MB.";
echo $message;
//File Type/Extension Check
}else if(!in_array($file_ext,$FILE_EXTS)){
$message =" <br>Sorry, $file_name($file_type) is not allowed to be uploaded.";
echo $message;
}else{
 if (copy($file['tmp_name'], $uploadfile)) {
  print "上传成功: $uploadfile";
//   print "<br>Here's some more debugging info:/n";
  unlink($file['tmp_name']);
//   print_r($_FILES);
 } else {
  print "上传失败/n";
//   print_r($_FILES);
 }
}

  }
}

print "</pre>";

exit;
?>

<?php
$file_count = count($_FILES['userfile']['name']);
echo "<br>".$file_count." files<br>";

for($x=0;$x<$file_count;$x++){
 print 'File Name: ' . $_FILES['userfile']['name'][$x].'<br>';
}
?>
<form enctype="multipart/form-data" action="up.php" method="post">
Send this file: <input name="userfile[]" type="file" /><br>
Send this file: <input name="userfile[]" type="file" /><br>
Send this file: <input name="userfile[]" type="file" /><br>
<input type="submit" value="Send File" />
</form>
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:7724次
    • 积分:129
    • 等级:
    • 排名:千里之外
    • 原创:4篇
    • 转载:2篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档
    推荐