php download function

不言
Release: 2023-03-24 12:30:02
Original
2472 people have browsed it

The content introduced in this article is about the function of php download, which has certain reference value. Now I share it with everyone. Friends in need can refer to it

upload.php starts

<?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;
  }
}
?>
Copy after login

upload.php ends

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));
?>
Copy after login

doUpload.php ends

Related recommendations:

php download function

PHP printing problem

The above is the detailed content of php download function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!