首頁 > 後端開發 > php教程 > (進階篇)PHP的檔案上傳與下載實例

(進階篇)PHP的檔案上傳與下載實例

黄舟
發布: 2023-03-05 14:06:01
原創
2002 人瀏覽過

以下正文:

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中文網(www.php.cn)!


來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板