linux社区爱心援助Linux认证系列教程业界动态站务新闻公司招聘建议留言网址大全LPI专题CISCO专题
设为首页
加入收藏
管理团队
JSP  
JAVA  
PERL  
 您的位置:首页 > 开发语言 > php >
栏目导栏
  php
  JSP
  ASP
  asp.net
  JAVA
  c/c++/c#
  perl
  JavaScript
  Basic
  Delphi
资料搜索
热门文章
·PHP生成静态页面的一些经验
·PHP无限分类与树型论坛的实现方
·php5学习笔记
·入门级PHP程序员面试题
·PHP 和 MySQL初学入门
·PHP入门速成
·vim的代码折叠
·文件下载统计php编程代码
·如何建立自己的新闻发布系统
·织梦CMS中文转换拼音函数研究
·Windows下Apache+Tomcat+MySQL
·PHP初学者头疼问题总结
·Cookie及其使用详细介绍
·生成sessionid和随机密码的例子
·使用无限生命期Session的方法
最新文章
·PHP入门速成
·用php实现广告轮播
·Zend Optimizer 问题浅析
·功能强大的CGI语言----PHP3
·用Session对Web页面进行保护
·PHP--进行模块化设计
·如何将PHP的结果输出到非PHP页
·如何开发一个虚拟域名系统
·PHP4调用自己编写的COM组件
·简单的页面缓冲技术(三)
·简单的页面缓冲技术(二)
·简单的页面缓冲技术(一)
·用Socket发送电子邮件(二)
·用Socket发送电子邮件(一)
·PHP/MySQL 购物车
Google
 
用PHP文件上传的具体思路及实现
[ 作者:  加入时间:2007-12-03 16:25:21  来自:Linux联盟收集整理 ]
文件上传我们需要用到HTML里面表单的type=file类型,及其enctype属性。这是我们大家必须要用的。当然了PHP函数库当中的FILE函数库,字符串类型函数库,目录函数库及$_FILES[]的使用是我们必须要用到的。 5LdLinux联盟
   5LdLinux联盟
    也许每一个站点都可能会对上传文件有许多的限制,这些限制会包括 文件类型,文件大小,扩展名,以及上传目录的存在与否,上传文件的存在与否,目录的可写性,可读性,上传文件的改名及怎样把文件从缓存当中复制到你所需要的目录当中。 5LdLinux联盟
   5LdLinux联盟
    当然出错的预处理也是我们不容忽视的!如果再深一步的讨论我们还可以对文件的操作起用事件日志的记录。 5LdLinux联盟
   5LdLinux联盟
    下面我们通过一段程序来实现这些功能: 5LdLinux联盟
     5LdLinux联盟
    首先是我们预设的变量值,它包括文件大小,文件扩展名类型,MIMI类型,及是否删除的开关变量 5LdLinux联盟
   5LdLinux联盟
  $MAX_SIZE = 2000000; 5LdLinux联盟
  $FILE_MIMES = array('image/jpeg','image/jpg','image/gif' 5LdLinux联盟
  ,'image/png','application/msword'); 5LdLinux联盟
   5LdLinux联盟
  $FILE_EXTS = array('.zip','.jpg','.png','.gif'); 5LdLinux联盟
   5LdLinux联盟
  $DELETABLE = true; 5LdLinux联盟
   5LdLinux联盟
    下一部就是设置浏览器访问变量及目录访问变量: 5LdLinux联盟
   5LdLinux联盟
  $site_name = $_SERVER['HTTP_HOST']; 5LdLinux联盟
  $url_dir = http://.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']); 5LdLinux联盟
  $url_this = http://.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']; 5LdLinux联盟
   5LdLinux联盟
  $upload_dir = files/; 5LdLinux联盟
  $upload_url = $url_dir./files/; 5LdLinux联盟
  $message =; 5LdLinux联盟
   5LdLinux联盟
    建立上传目录并相应改变权限: 5LdLinux联盟
   5LdLinux联盟
  if (!is_dir(files)) { 5LdLinux联盟
   if (!mkdir($upload_dir)) 5LdLinux联盟
    die (upload_files directory doesn't exist and creation failed); 5LdLinux联盟
   if (!chmod($upload_dir,0755)) 5LdLinux联盟
    die (change permission to 755 failed.); 5LdLinux联盟
  } 5LdLinux联盟
   5LdLinux联盟
    用户请求的处理: 5LdLinux联盟
   5LdLinux联盟
   5LdLinux联盟
  if ($_REQUEST[del] && $DELETABLE) { 5LdLinux联盟
   $resource = fopen(log.txt,a); 5LdLinux联盟
   fwrite($resource,date(Ymd h:i:s).DELETE - $_SERVER[REMOTE_ADDR].$_REQUEST[del]\\n); 5LdLinux联盟
   fclose($resource); 5LdLinux联盟
   5LdLinux联盟
   if (strpos($_REQUEST[del],/.)>0); //possible hacking 5LdLinux联盟
   else if (strpos($_REQUEST[del],files/) === false); //possible hacking 5LdLinux联盟
   else if (substr($_REQUEST[del],0,6)==files/) { 5LdLinux联盟
    unlink($_REQUEST[del]); 5LdLinux联盟
    print <script>window.location.href='$url_this?message=deleted successfully'</script>; 5LdLinux联盟
   } 5LdLinux联盟
  } 5LdLinux联盟
  else if ($_FILES['userfile']) { 5LdLinux联盟
   $resource = fopen(log.txt,a); 5LdLinux联盟
   fwrite($resource,date(Ymd h:i:s).UPLOAD - $_SERVER[REMOTE_ADDR] 5LdLinux联盟
   .$_FILES['userfile']['name']. 5LdLinux联盟
   .$_FILES['userfile']['type'].\\n); 5LdLinux联盟
   fclose($resource); 5LdLinux联盟
   5LdLinux联盟
   $file_type = $_FILES['userfile']['type']; 5LdLinux联盟
   $file_name = $_FILES['userfile']['name']; 5LdLinux联盟
   $file_ext = strtolower(substr($file_name,strrpos($file_name,.))); 5LdLinux联盟
   5LdLinux联盟
   //文件大小的检查: 5LdLinux联盟
   5LdLinux联盟
   if ( $_FILES['userfile']['size'] > $MAX_SIZE) 5LdLinux联盟
    $message = The file size is over 2MB.; 5LdLinux联盟
    //File Type/Extension Check 5LdLinux联盟
   else if (!in_array($file_type, $FILE_MIMES) 5LdLinux联盟
  && !in_array($file_ext, $FILE_EXTS) ) 5LdLinux联盟
    $message = Sorry, $file_name($file_type) is not allowed to be uploaded.; 5LdLinux联盟
   else 5LdLinux联盟
    $message = do_upload($upload_dir, $upload_url); 5LdLinux联盟
   5LdLinux联盟
   print <script>window.location.href='$url_this?message=$message'</script>; 5LdLinux联盟
  } 5LdLinux联盟
  else if (!$_FILES['userfile']); 5LdLinux联盟
  else 5LdLinux联盟
  $message = Invalid File Specified.; 5LdLinux联盟
   5LdLinux联盟
    列出我们上传的文件: 5LdLinux联盟
   5LdLinux联盟
  $handle=opendir($upload_dir); 5LdLinux联盟
  $filelist = ; 5LdLinux联盟
  while ($file = readdir($handle)) { 5LdLinux联盟
   if(!is_dir($file) && !is_link($file)) { 5LdLinux联盟
    $filelist .= <a href='$upload_dir$file'>.$file.</a>; 5LdLinux联盟
   if ($DELETABLE) 5LdLinux联盟
    $filelist .= <a href='?del=$upload_dir$file' title='delete'>x</a>; 5LdLinux联盟
    $filelist .= <sub><small><small><font color=grey> .date(d-m H:i, filemtime($upload_dir.$file)) 5LdLinux联盟
  .</font></small></small></sub>; 5LdLinux联盟
    $filelist .=<br>; 5LdLinux联盟
   } 5LdLinux联盟
  } 5LdLinux联盟
   5LdLinux联盟
  function do_upload($upload_dir, $upload_url) { 5LdLinux联盟
   5LdLinux联盟
   $temp_name = $_FILES['userfile']['tmp_name']; 5LdLinux联盟
   $file_name = $_FILES['userfile']['name']; 5LdLinux联盟
   $file_name = str_replace(\\\\,,$file_name); 5LdLinux联盟
   $file_name = str_replace(',,$file_name); 5LdLinux联盟
   $file_path = $upload_dir.$file_name; 5LdLinux联盟
   5LdLinux联盟
   //File Name Check 5LdLinux联盟
   if ( $file_name ==) { 5LdLinux联盟
    $message = Invalid File Name Specified; 5LdLinux联盟
    return $message; 5LdLinux联盟
   } 5LdLinux联盟
   5LdLinux联盟
   $result = move_uploaded_file($temp_name, $file_path); 5LdLinux联盟
   if (!chmod($file_path,0777)) 5LdLinux联盟
    $message = change permission to 777 failed.; 5LdLinux联盟
   else 5LdLinux联盟
    $message = ($result)?$file_name uploaded successfully. : 5LdLinux联盟
   Somthing is wrong with uploading a file.; 5LdLinux联盟
   return $message; 5LdLinux联盟
  } 5LdLinux联盟
   5LdLinux联盟
  ?> 5LdLinux联盟
   5LdLinux联盟
  <center> 5LdLinux联盟
  <font color=red><?=$_REQUEST[message]?></font> 5LdLinux联盟
  <br> 5LdLinux联盟
  <form name=upload id=upload ENCTYPE=multipart/form-data method=post> 5LdLinux联盟
  Upload File <input type=file id=userfile name=userfile> 5LdLinux联盟
  <input type=submit name=upload value=Upload> 5LdLinux联盟
  </form> 5LdLinux联盟
   5LdLinux联盟
  <br><b>My Files</b> 5LdLinux联盟
  <hr width=70%> 5LdLinux联盟
  <?=$filelist?> 5LdLinux联盟
  <hr width=70%> 5LdLinux联盟
  <small><sup>Developed By 5LdLinux联盟
  <a style=text-decoration:none href=http://tech.citypost.ca>CityPost.ca</a> 5LdLinux联盟
  </sup></small> 5LdLinux联盟
  </center> 5LdLinux联盟
Linux联盟收集整理 ,转贴请标明原始链接,如有任何疑问欢迎来本站Linux论坛讨论
评论】【加入收藏夹】【 】【打印】【关闭
※ 相关链接
 ·PHP经典的给图片加水印程序  (2007-12-03 16:22:23)
 ·使用PHP往Windows系统中添加用户  (2007-12-03 16:02:29)
 ·轻型数据库SQLite结合PHP的开发  (2007-12-03 16:00:56)
 ·PHP实现自动刷数和“灌水”机  (2007-12-03 16:00:21)
 ·PHP实现网页自动更新块  (2007-12-03 15:59:55)
 ·用PHP读写NTFS文件系统下的文件摘要信息  (2007-12-03 15:57:11)
 ·PHP中通过Web执行C/C++应用程序  (2007-12-03 15:55:58)
 ·PHP下实现端口复用/劫持  (2007-12-03 15:54:52)
 ·PHP和JAVA的XML-RPC中文问题解决办法  (2007-12-03 15:54:04)
 ·使用php的zlib压缩和解压缩swf文件  (2007-11-29 07:29:43)