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

文件上传我们需要用到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]\n);
 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].\n);
 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 .= <a href=$upload_dir$file>.$file.</a>;
 if ($deletable)
  $filelist .= <a href=?del=$upload_dir$file title=delete>x</a>;
  $filelist .= <sub><small><small><font color=grey> .date(d-m h:i, filemtime($upload_dir.$file))
.</font></small></small></sub>;
  $filelist .=<br>;
 }
}

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;
}

?>

<center>
<font color=red><?=$_request[message]?></font>
<br>
<form name=upload id=upload enctype=multipart/form-data method=post>
upload file <input type=file id=userfile name=userfile>
<input type=submit name=upload value=upload>
</form>

<br><b>my files</b>
<hr width=70%>
<?=$filelist?>
<hr width=70%>
<small><sup>developed by
<a style=text-decoration:none href=http://tech.citypost.ca>citypost.ca</a>
</sup></small>
</center>

http://www.bkjia.com/phpjc/508470.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/508470.htmltecharticle文件上传我们需要用到html里面表单的type=file类型,及其enctype属性。这是我们大家必须要用的。当然了php函数库当中的file函数库,字符串类…

Posted in 未分类

发表评论