PHP上传下载

  1. php文件上传

     <?php
     $file = $_FILES['upload'] ? $_FILES['upload'] : '';
     if (is_uploaded_file($file)) {
         $name = $file['name'];
         $type = $file['type'];
         $tmp_name = $file['tmp_name'];
         $error = $file['error'];
         $size = $file['size'];
         if ($error !== 0) {
             die('上传失败');
         }
         if ($type != 'png') {
             die('图片格式必须为png');
         }
         if ($size > 1024 * 1024) {
             die('图片大小不能超过1M');
         }
         $path = '/var/www/progect/picture/123.png';
         move_uploaded_file($tmp_name,$path);
     }
     die('上传失败');
    
  2. php文件断点续传下载

     <?php
     function getFile($path){
         $code = mb_detect_encoding($path);
         if($code != 'gb2312'){
             $path=iconv($code,"gb2312",$path);
         }
         if(ini_get('zlib.output_compression')){
             ini_set('zlib.output_compression', 'Off');
         }
         if(file_exists($path)){
             $size = filesize($path);
             $basename = basename($path);
             $path_parts = pathinfo($path);
             $ext = strtolower($path_parts["extension"]);
             switch ($ext) {
                 case "gif" : $ctype="image/gif";break;
                 case "png" : $ctype="image/png";break;
                 case "jpeg": $ctype="image/jpg";break;
                 case "jpg" : $ctype="image/jpg";break;
                 case "pdf" : $ctype="application/pdf";break;
                 case "zip" : $ctype="application/zip";break;
                 case "doc" : $ctype="application/msword";break;
                 case "exe" : $ctype="application/octet-stream";break;
                 case "xls" : $ctype="application/vnd.ms-excel";break;
                 case "ppt" : $ctype="application/vnd.ms-powerpoint";break;
                 default    : $ctype="application/force-download";
             }
             if(!headers_sent()){
                 header("Expires: 0");
                 header("Pragma: public");
                 if(isset($_SERVER['HTTP_RANGE'])){
                     $range = explode('-',str_replace('=','-',$_SERVER['HTTP_RANGE']));
                     $point = trim($range[1]);
                     header('HTTP /1.1 206 Partial Content');
                     header('Content-Range: bytes ' . $range . '-' . $size-1 . '/' . $size);
                 }else{
                     header('Content-Range: bytes 0-' . $size-1 . '/' . $size);
                 }
                 header("Content-Length: ".$size);
                 header("Cache-Control: private",false);
                 header("Content-Transfer-Encoding: binary");
                 header("Content-Type: charset=utf-8;".$ctype);
                 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
                 header("Content-Disposition: attachment;filename='$basename';");
             }
             ob_clean();
             flush();
             readfile($path);
         }
     }
     getFile('C:\Users\geng\Desktop\中文.log');
    
@耿志环 2012-∞ 冀ICP备17033181号, powered by Gitbook修订: 2019-01-11 16:52:19

results matching ""

    No results matching ""