首页  >  文章  >  后端开发  >  php下载的功能

php下载的功能

不言
不言原创
2018-04-19 15:13:012494浏览

这篇文章介绍的内容是关于php下载的功能,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

upload.php开始

<?php
function upload_file($fileInfo,$uploadPath=&#39;./uploads&#39;,$imageFlag=true,$allowExt=array(&#39;jpeg&#39;,&#39;jpg&#39;,&#39;png&#39;,&#39;gif&#39;),$maxSize=209708){
	define(&#39;UPLOAD_ERRS&#39;,[
	    &#39;upload_max_filesize&#39;=>&#39;超过了PHP配置文件中upload_max_filesize选项的值&#39;,
	    &#39;form_max_size&#39;=>&#39;超过了表单MAX_FILE_SIZE选项的值&#39;,
	    &#39;upload_file_partial&#39;=>&#39;文件部分被上传&#39;,
	    &#39;no_upload_file_select&#39;=>&#39;没有选择上传文件&#39;,
	    &#39;upload_system_error&#39;=>&#39;系统错误&#39;,
	    &#39;no_allow_ext&#39;=>&#39;非法文件类型&#39;,
	    &#39;exceed_max_size&#39;=>&#39;超出允许上传的最大值&#39;,
	    &#39;not_true_image&#39;=>&#39;文件不是真实图片&#39;,
	    &#39;not_http_post&#39;=>&#39;文件不是通过HTTP POST方式上传上来的&#39;,
	    &#39;move_error&#39;=>&#39;文件移动失败&#39;
	]);
//检测是否上传是否有错误
  if($fileInfo[&#39;error&#39;]===UPLOAD_ERR_OK){
    //检测上传文件类型
    $ext=strtolower(pathinfo($fileInfo[&#39;name&#39;],PATHINFO_EXTENSION));
    if(!in_array($ext,$allowExt)){
      echo  UPLOAD_ERRS[&#39;no_allow_ext&#39;];
      return false;
    }
    //检测上传文件大小是否符合规范
    if($fileInfo[&#39;size&#39;]>$maxSize){
      echo UPLOAD_ERRS[&#39;exceed_max_size&#39;];
      return false;
    }
    //检测是否是真实图片
    if($imageFlag){
      if(@!getimagesize($fileInfo[&#39;tmp_name&#39;])){
        echo UPLOAD_ERRS[&#39;not_true_image&#39;];
        return false;
      }
    }
    //检测文件是否通过HTTP POST方式上传上来的
    if(!is_uploaded_file($fileInfo[&#39;tmp_name&#39;])){
      return UPLOAD_ERRS[&#39;not_http_post&#39;];
    }
    //检测目标目录是否存在,不存在则创建
    if(!is_dir($uploadPath)){
      mkdir($uploadPath,0777,true);
    }
    //生成唯一文件名,防止重名产生覆盖
    $uniName=md5(uniqid(microtime(true),true)).&#39;.&#39;.$ext;
    $dest=$uploadPath.DIRECTORY_SEPARATOR.$uniName;


    //移动文件
    if(@!move_uploaded_file($fileInfo[&#39;tmp_name&#39;],$dest)){
      echo UPLOAD_ERRS[&#39;move_error&#39;];
      return false;
    }
    echo &#39;文件上传成功&#39;;
    return $dest;
  }else{
    switch($fileInfo[&#39;error&#39;]){
      case 1:
      // $mes=&#39;超过了PHP配置文件中upload_max_filesize选项的值&#39;;
      $mes=UPLOAD_ERRS[&#39;upload_max_filesize&#39;];
      break;
      case 2:
      $mes=UPLOAD_ERRS[&#39;form_max_size&#39;];
      break;
      case 3:
      $mes=UPLAOD_ERRS[&#39;upload_file_partial&#39;];
      break;
      case 4:
      $mes=UPLOAD_ERRS[&#39;no_upload_file_select&#39;];
      break;
      case 6:
      case 7:
      case 8:
      $mes=UPLAOD_ERRS[&#39;upload_system_error&#39;];
      break;
    }
    echo $mes;
    return false;
  }
}
?>

upload.php结束

html的开始
<!DOCTYPE html>
<html>
<head>
 <meta charset="UTF-8">
 <title>Document</title>
</head>
<body>
 <!-- <a href="http://localhost:8081/download.php?filename=5.html">下载5.html</a> -->
 <h1>文件上传</h1>
 <form action="http://localhost:8081/doUpload.php" method=&#39;post&#39; enctype="multipart/form-data">
 <input type="file" name="myFile" id="">
 <input type="submit" value="立即上传">
 </form>
</body>
</html>
html的结束

doUpload.php的开始
<?php
 require_once(&#39;upload.php&#39;);
 $fileInfo=$_FILES[&#39;myFile&#39;];
var_dump(upload_file($fileInfo));
?>

doUpload.php的结束

相关推荐:

php的下载功能

PHP的打印问题

以上是php下载的功能的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn