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

最近一段时间对php文件处理方面很感兴趣,因此在许多站点上看了许多的文件处理的文章,但是国内许多的站点上的php文件处理方面的知识大多数是你抄我的我抄你的,用baidu.com或者是google.com搜索出来的东西多是重复的。最近在国外一个站点上盾了一篇文章感觉很不错,因此推荐给大家阅读。首先我们有必要说明一下文件上传的操作流程及用到的知识点:文件上传我们需要用到html里面表单的type=”file”类型,及其enctype属性。这是我们大家必须要用的。当然了php函数库当中的file函数库,字符串类型函数库,目录函数库及$_files[]的使用是我们必须要用到的。也许每一个站点都可能会对上传文件有许多的限制,这些限制会包括 文件类型,文件大小,扩展名,以及上传目录的存在与否,上传文件的存在与否,目录的可写性,可读性,上传文件的改名及怎样把文件从缓存当中复制到你所需要的目录当中。当然出错的预处理也是我们不容忽视的!如果再深一步的讨论我们还可以对文件的操作起用事件日志的记录。下面我们通过一段程序来实现这些功能:——————————————————————————————–首先是我们预设的变量值,它包括文件大小,文件扩展名类型,mimi类型,及是否删除的开关变量$max_size = 2000000;$file_mimes = array(image/jpeg,image/jpg,image/gif ,image/png,application/msword);$file_exts = array(.zip,.jpg,.png,.gif); $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 =””;建立上传目录并相应改变权限:if (!is_dir(“files”)) { if (!mkdir($upload_dir)) die (“upload_files directory doesnt exist and creation failed”); if (!chmod($upload_dir,0755)) die (“change permission to 755 failed.”);}用户请求的处理:if ($_request[del] && $deletable) { $resource = fopen(“log.txt”,”a”); fwrite($resource,date(“ymd h:i:s”).”delete – $_server[remote_addr]”.”$_request[del]
“); fclose($resource); if (strpos($_request[del],”/.”)>0); //possible hacking else if (strpos($_request[del],”files/”) === false); //possible hacking else if (substr($_request[del],0,6)==”files/”) { unlink($_request[del]); print “《script》window.location.href=$url_this?message=deleted successfully《script》”; }}else if ($_files[userfile]) { $resource = fopen(“log.txt”,”a”); fwrite($resource,date(“ymd h:i:s”).”upload – $_server[remote_addr]” .$_files[userfile][name].” ” .$_files[userfile][type].”
“); fclose($resource);$file_type = $_files[userfile][type]; $file_name = $_files[userfile][name]; $file_ext = strtolower(substr($file_name,strrpos($file_name,”.”))); //文件大小的检查: if ( $_files[userfile][size] > $max_size) $message = “the file size is over 2mb.”; //file type/extension check else if (!in_array($file_type, $file_mimes) && !in_array($file_ext, $file_exts) ) $message = “sorry, $file_name($file_type) is not allowed to be uploaded.”; else $message = do_upload($upload_dir, $upload_url); print “《script》window.location.href=$url_this?message=$message《script》”;}else if (!$_files[userfile]);else $message = “invalid file specified.”;列出我们上传的文件:$handle=opendir($upload_dir);$filelist = “”;while ($file = readdir($handle)) { if(!is_dir($file) && !is_link($file)) { $filelist .= “”.$file.””; if ($deletable) $filelist .= ” x”; $filelist .= ” “.date(“d-m h:i”, filemtime($upload_dir.$file)) .””; $filelist .=””; }}function do_upload($upload_dir, $upload_url) {$temp_name = $_files[userfile][tmp_name];$file_name = $_files[userfile][name]; $file_name = str_replace(“\”,””,$file_name); $file_name = str_replace(“”,””,$file_name);$file_path = $upload_dir.$file_name;//file name check if ( $file_name ==””) { $message = “invalid file name specified”; return $message; } $result = move_uploaded_file($temp_name, $file_path); if (!chmod($file_path,0777)) $message = “change permission to 777 failed.”; else $message = ($result)?”$file_name uploaded successfully.” : “somthing is wrong with uploading a file.”; return $message;}?> upload file my files developed by citypost.ca

http://www.bkjia.com/phpjc/532625.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/532625.htmltecharticle最近一段时间对php文件处理方面很感兴趣,因此在许多站点上看了许多的文件处理的文章,但是国内许多的站点上的php文件处理方面的知识…

Posted in 未分类

发表评论