(Advanced) PHP file upload and download examples

黄舟
Release: 2023-03-05 14:06:01
Original
1993 people have browsed it

The following text:

1. First request the page 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>
Copy after login

2.php to process the data requested by the client 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>
Copy after login

3. PHP file download

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

The above is the (advanced) content of PHP file upload and download examples. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


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