Rumah  >  Artikel  >  pembangunan bahagian belakang  >  (进阶篇)PHP的文件上传与下载实例

(进阶篇)PHP的文件上传与下载实例

黄舟
黄舟asal
2017-02-07 09:44:431983semak imbas

以下正文:

1.先来个请求页面upload.html

<html>  
<head>  
  <title>Administration - upload new files</title>  
</head>  
<body>  
<h1>Upload new news files</h1>  
<form enctype="multipart/form-data" action="upload.php" method=post>  
  <input type="hidden" name="MAX_FILE_SIZE" value="1000000">  
  Upload this file: <input name="userfile" type="file">  
  <input type="submit" value="Send File">  
</form>  
</body>  
</html>

2.php处理客户端请求的数据upload.php

<html>  
<head>  
  <title>Uploading...</title>  
</head>  
<body>  
<h1>Uploading file...</h1>  
<?php  
  
//Check to see if an error code was generated on the upload attempt  
  if ($_FILES[&#39;userfile&#39;][&#39;error&#39;] > 0)  
  {  
    echo &#39;Problem: &#39;;  
    switch ($_FILES[&#39;userfile&#39;][&#39;error&#39;])  
    {  
      case 1:   echo &#39;File exceeded upload_max_filesize&#39;;  
                break;  
      case 2:   echo &#39;File exceeded max_file_size&#39;;  
                break;  
      case 3:   echo &#39;File only partially uploaded&#39;;  
                break;  
      case 4:   echo &#39;No file uploaded&#39;;  
                break;  
      case 6:   echo &#39;Cannot upload file: No temp directory specified.&#39;;  
                break;  
      case 7:   echo &#39;Upload failed: Cannot write to disk.&#39;;  
                break;  
    }  
    exit;  
  }  
  
  // Does the file have the right MIME type?  
  if ($_FILES[&#39;userfile&#39;][&#39;type&#39;] != &#39;text/plain&#39;)  
  {  
    echo &#39;Problem: file is not plain text&#39;;  
    exit;  
  }  
  
  // put the file where we&#39;d like it  
  $upfile = &#39;/uploads/&#39;.$_FILES[&#39;userfile&#39;][&#39;name&#39;];  
  
  if (is_uploaded_file($_FILES[&#39;userfile&#39;][&#39;tmp_name&#39;]))   
  {  
     if (!move_uploaded_file($_FILES[&#39;userfile&#39;][&#39;tmp_name&#39;], $upfile))  
     {  
        echo &#39;Problem: Could not move file to destination directory&#39;;  
        exit;  
     }  
  }   
  else   
  {  
    echo &#39;Problem: Possible file upload attack. Filename: &#39;;  
    echo $_FILES[&#39;userfile&#39;][&#39;name&#39;];  
    exit;  
  }  
  
  
  echo &#39;File uploaded successfully<br><br>&#39;;   
  
  // reformat the file contents  
  $fp = fopen($upfile, &#39;r&#39;);  
  $contents = fread ($fp, filesize ($upfile));  
  fclose ($fp);  
   
  $contents = strip_tags($contents);  
  $fp = fopen($upfile, &#39;w&#39;);  
  fwrite($fp, $contents);  
  fclose($fp);  
  
  // show what was uploaded  
  echo &#39;Preview of uploaded file contents:<br><hr>&#39;;  
  echo $contents;  
  echo &#39;<br><hr>&#39;;  
  
?>  
</body>  
</html>

3.php文件下载

<?php  
  
    $filePath = "template/";//此处给出你下载的文件在服务器的什么地方  
    $fileName = "template.xls";  
    //此处给出你下载的文件名  
    $file = fopen($filePath . $fileName, "r"); //   打开文件  
    //输入文件标签  
    Header("Content-type:application/octet-stream ");  
    Header("Accept-Ranges:bytes ");  
    Header("Accept-Length:   " . filesize($filePath . $fileName));  
    Header("Content-Disposition:   attachment;   filename= " . $fileName);  
      
    //   输出文件内容  
    echo fread($file, filesize($filePath . $fileName));  
    fclose($file);  
    exit;  
  
?>

以上就是(进阶篇)PHP的文件上传与下载实例的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn