用PHP文件上传的具体思路及实现

林继 PHP 技术 5,751 共写了583个字 (2006-02-27 13:23:19) 没有评论 打印 扫描二维码 百度已收录

From:http://bbs.dzfly.com/read.php?tid=15198

文件上传我们需要用到HTML里面表单的type=file类型,及其enctype属性。这是我们大家必须要用的。当然了PHP函数库当中的FILE函数库,字符串类型函数库,目录函数库及$_FILES[]的使用是我们必须要用到的。

  也许每一个站点都可能会对上传文件有许多的限制,这些限制会包括 文件类型,文件大小,扩展名,以及上传目录的存在与否,上传文件的存在与否,目录的可写性,可读性,上传文件的改名及怎样把文件从缓存当中复制到你所需要的目录当中。

当然出错的预处理也是我们不容忽视的!如果再深一步的讨论我们还可以对文件的操作起用事件日志的记录。

下面我们通过一段程序来实现这些功能:
  
首先是我们预设的变量值,它包括文件大小,文件扩展名类型,MIMI类型,及是否删除的开关变量

  1. $MAX_SIZE = 2000000;
  2. $FILE_MIMES = array('image/jpeg','image/jpg','image/gif'
  3. ,'image/png','application/msword');
  4.  
  5. $FILE_EXTS = array('.zip','.jpg','.png','.gif');
  6.  
  7. $DELETABLE = true;

下一部就是设置浏览器访问变量及目录访问变量:

  1. $site_name = $_SERVER['HTTP_HOST'];
  2. $url_dir = $_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'">http://.$_SERVER华北资源在线['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
  3. $url_this = $_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'">http://.$_SERVER华北资源在线['HTTP_HOST'].$_SERVER['PHP_SELF'];
  4.  
  5. $upload_dir = files/;
  6. $upload_url = $url_dir./files/;
  7. $message =;

建立上传目录并相应改变权限:

  1. if (!is_dir(files)) {
  2.  if (!mkdir($upload_dir))
  3.   die (upload_files directory doesn't exist and creation failed);
  4.  if (!chmod($upload_dir,0755))
  5.   die (change permission to 755 failed.);
  6. }

用户请求的处理:

  1. if ($_REQUEST[del] && $DELETABLE) {
  2.  $resource = fopen(log.txt,a);
  3.  fwrite($resource,date(Ymd h:i:s).DELETE - $_SERVER[REMOTE_ADDR].$_REQUEST[del]\\n);
  4.  fclose($resource);
  5.  
  6.  if (strpos($_REQUEST[del],/.)>0); //possible hacking
  7.  else if (strpos($_REQUEST[del],files/) === false); //possible hacking
  8.  else if (substr($_REQUEST[del],0,6)==files/) {
  9.   unlink($_REQUEST[del]);
  10.   print <script>window.location.href='$url_this?message=deleted successfully'</script>;
  11.  }
  12. }
  13. else if ($_FILES['userfile']) {
  14.  $resource = fopen(log.txt,a);
  15.  fwrite($resource,date(Ymd h:i:s).UPLOAD - $_SERVER[REMOTE_ADDR]
  16.  .$_FILES['userfile']['name'].
  17.  .$_FILES['userfile']['type'].\\n);
  18.  fclose($resource);
  19.  
  20.  $file_type = $_FILES['userfile']['type'];
  21.  $file_name = $_FILES['userfile']['name'];
  22.  $file_ext = strtolower(substr($file_name,strrpos($file_name,.)));
  23. //文件大小的检查:
  24.  
  25.  if ( $_FILES['userfile']['size'] > $MAX_SIZE)
  26.   $message = The file size is over 2MB.;
  27.   //File Type/Extension Check
  28.  else if (!in_array($file_type, $FILE_MIMES)
  29. && !in_array($file_ext, $FILE_EXTS) )
  30.   $message = Sorry, $file_name($file_type) is not allowed to be uploaded.;
  31.  else
  32.   $message = do_upload($upload_dir, $upload_url);
  33.  
  34.  print <script>window.location.href='$url_this?message=$message'</script>;
  35. }
  36. else if (!$_FILES['userfile']);
  37. else
  38. $message = Invalid File Specified.;

列出我们上传的文件:

  1. $handle=opendir($upload_dir);
  2. $filelist = ;
  3. while ($file = readdir($handle)) {
  4.  if(!is_dir($file) && !is_link($file)) {
  5.   $filelist .= <a href='$upload_dir$file'>.$file.</a>;
  6.  if ($DELETABLE)
  7.   $filelist .= <a href='?del=$upload_dir$file' title='delete'>x</a>;
  8.   $filelist .= <sub><small><small><font color=grey> .date(d-m H:i, filemtime($upload_dir.$file))
  9. .</font></small></small></sub>;
  10.   $filelist .=<br>;
  11.  }
  12. }
  13.  
  14. function do_upload($upload_dir, $upload_url) {
  15.  
  16.  $temp_name = $_FILES['userfile']['tmp_name'];
  17.  $file_name = $_FILES['userfile']['name'];
  18.  $file_name = str_replace(\\\\,,$file_name);
  19.  $file_name = str_replace(',,$file_name);
  20.  $file_path = $upload_dir.$file_name;
  21.  
  22.  //File Name Check
  23.  if ( $file_name ==) {
  24.   $message = Invalid File Name Specified;
  25.   return $message;
  26.  }
  27.  
  28.  $result = move_uploaded_file($temp_name, $file_path);
  29.  if (!chmod($file_path,0777))
  30.   $message = change permission to 777 failed.;
  31.  else
  32.   $message = ($result)?$file_name uploaded successfully. :
  33.  Somthing is wrong with uploading a file.;
  34.  return $message;
  35. }
  36.  
  37. ?>
  38.  
  39. <center>
  40. <font color=red><?=$_REQUEST[message]?></font>
  41. <br>
  42. <form name=upload id=upload ENCTYPE=multipart/form-data method=post>
  43. Upload File <input type=file id=userfile name=userfile>
  44. <input type=submit name=upload value=Upload>
  45. </form>
  46.  
  47. <br><b>My Files</b>
  48. <hr width=70%>
  49. <?=$filelist?>
  50. <hr width=70%>
  51. <small><sup>Developed By
  52. <a style=text-decoration:none href=http://tech.citypost.ca>CityPost.ca</a>
  53. </sup></small>
  54. </center>

如果觉得我的文章对您有用,请随意赞赏。您的支持将鼓励我继续创作!

发表评论

电子邮件地址不会被公开。 必填项已用*标注

< >