|
 |
栏目导栏 |
|
| |
|
|
|
|
 |
资料搜索 |
|
| |
|
|
|
|
 |
热门文章 |
|
| |
|
|
|
|
 |
最新文章 |
|
| |
|
|
|
| |
| |
|
|
|
|
文件上传我们需要用到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论坛讨论 |
|
|
|
|
|