(고급) PHP 파일 업로드 및 다운로드 예제

黄舟
풀어 주다: 2023-03-05 14:06:01
원래의
1935명이 탐색했습니다.

다음 텍스트:

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)를 참고해주세요!


원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!