PHP實作檔案上傳下載的方法

墨辰丷
發布: 2023-03-28 17:36:01
原創
6053 人瀏覽過

這篇文章主要介紹了PHP實現文件上傳下載實例,本文詳細全面的講解了文件上傳相關的需求分析及功能實現,並同時給出了使用代碼,需要的朋友可以參考下。 PHP實作檔案上傳與下載

一、上傳原理與設定

#1.1 原理

將客戶端檔案上傳到伺服器端,再將伺服器端的檔案(暫存檔案)移到指定目錄即可。

1.2 用戶端設定

所需:表單頁面(選擇上傳檔案);

具體而言:傳送方式為POST,新增enctype="multipart/form- data"屬性,兩者缺一不可(但是,優缺點並存,這裡也限定了上傳的方式和上傳的文件之後的調用等方面,後面會說到)

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="doAction.php" method="post" enctype="multipart/form-data">
请选择您要上传的文件:
<input type="file" name="myFile" /><br/>
<input type="submit" value="上传"/>
</form>
<?php

?>
</body>
</html>
登入後複製

先是表單頁面(請自動忽略前端問題。。。),關鍵就是form的屬性;另外就是input 中用到了type="file"這一點(體現到php的強大的拓展等等)。

然後doAction

<?php
//$_FILES:文件上传变量
//print_r($_FILES);
$filename=$_FILES[&#39;myFile&#39;][&#39;name&#39;];
$type=$_FILES[&#39;myFile&#39;][&#39;type&#39;];
$tmp_name=$_FILES[&#39;myFile&#39;][&#39;tmp_name&#39;];
$size=$_FILES[&#39;myFile&#39;][&#39;size&#39;];
$error=$_FILES[&#39;myFile&#39;][&#39;error&#39;];

//将服务器上的临时文件移动到指定位置
//方法一move_upload_file($tmp_name,$destination)
//move_uploaded_file($tmp_name, "uploads/".$filename);//文件夹应提前建立好,不然报错
//方法二copy($src,$des)
//以上两个函数都是成功返回真,否则返回false
//copy($tmp_name, "copies/".$filename);
//注意,不能两个方法都对临时文件进行操作,临时文件似乎操作完就没了,我们试试反过来
copy($tmp_name, "copies/".$filename);
move_uploaded_file($tmp_name, "uploads/".$filename);
//能够实现,说明move那个函数基本上相当于剪切;copy就是copy,临时文件还在

//另外,错误信息也是不一样的,遇到错误可以查看或者直接报告给用户
if ($error==0) {
  echo "上传成功!";
}else{
  switch ($error){
    case 1:
      echo "超过了上传文件的最大值,请上传2M以下文件";
      break;
    case 2:
      echo "上传文件过多,请一次上传20个及以下文件!";
      break;
    case 3:
      echo "文件并未完全上传,请再次尝试!";
      break;
    case 4:
      echo "未选择上传文件!";
      break;
    case 5:
      echo "上传文件为0";
      break;
  }
}
登入後複製

#先把print_r($_FILES)這個資訊看一下

#
Array
(
  [myFile] => Array
    (
      [name] => 梁博_简历.doc
      [type] => application/msword
      [tmp_name] => D:\wamp\tmp\php1D78.tmp
      [error] => 0
      [size] => 75776
    )

)
登入後複製

所以得到的是個二維數組,該怎麼用,都是基本的東西(其實我喜歡降維再用);

基本上是一眼就懂的東西,不囉嗦,關鍵有兩個:tmp_name臨時檔名;error報錯訊息(代號,後面可以利用);

然後這裡看一下doAction後面一部分,利用報錯訊息來回饋給用戶,需要說明的是為什麼報錯,和報錯訊息是什麼都;

1.3 關於報錯

##--報錯原因:

基本上都是超過或不符合伺服器關於上傳檔案的配置,那麼伺服器端配置有哪些呢?

先考慮上傳我們用了什麼? POST,upload

所以在php.ini找這麼幾項:

file_upload:On

upload_tmp_dir=--臨時檔案保存目錄;

upload_max_filesize=2M

max_file_uploads=20-允許一次上傳的最大檔案數量(注意和上面那個的區別,有沒有size,別亂想)

post_max_size=8M-post方式發送資料的最大值

其他相關設定

max_exectuion_time=-1-最大執行時間,避免程式不好佔用伺服器資源;

max_input_time=60

max_input_nesting_level=64-輸入巢狀深度;

memory_limit=128M-最大單執行緒的獨立記憶體使用量

總之都是有關資源的配置。

--錯誤編號

以下(偷懶)引自http://blog.sina.com.cn/s/blog_3cdfaea201008utf.html

  • UPLOAD_ERR_OK             值:0; 沒有錯誤發生,且檔案上傳成功。

  • UPLOAD_ERR_INI_SIZE      值:1; 上傳的檔案超過了 php.ini 中 upload_max_filesize 選項限制的值。

  • UPLOAD_ERR_FORM_SIZE  值:2; 上傳檔案的大小超過了 HTML 表單中 MAX_FILE_SIZE 選項指定的值。

  • UPLOAD_ERR_PARTIAL          價值:3; 檔只有部分上傳。

  • UPLOAD_ERR_NO_FILE          價值:4; 沒有檔案上傳。 

注意:這個錯誤訊息是第一步上傳的訊息,也就是上傳到臨時資料夾的情況,而不是move或copy的情況。

二、上傳相關限制

2.1 用戶端限制


<form action="doAction2.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="101321" />
登入後複製

##請選擇您要上傳的檔案:


<input type="file" name="myFile" accept="image/jpeg,image/gif,text/html"/><br/>
<input type="submit" value="上传"/>
</form>
登入後複製

這裡用input的屬性對上傳檔案的大小和類型進行了限制,但是個人感覺:一,html程式碼是「可見的」;二,常常不起作用(沒找到原因,但因為第一個我也想放棄它,知道就好。

 2.2 伺服器端限制

主要限制大小和類型,再有就是方式。


<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);
//接受文件,临时文件信息
$fileinfo=$_FILES["myFile"];//降维操作
$filename=$fileinfo["name"];
$tmp_name=$fileinfo["tmp_name"];
$size=$fileinfo["size"];
$error=$fileinfo["error"];
$type=$fileinfo["type"];

//服务器端设定限制
$maxsize=10485760;//10M,10*1024*1024
$allowExt=array(&#39;jpeg&#39;,&#39;jpg&#39;,&#39;png&#39;,&#39;tif&#39;);//允许上传的文件类型(拓展名
$ext=pathinfo($filename,PATHINFO_EXTENSION);//提取上传文件的拓展名

//目的信息
$path="uploads";
if (!file_exists($path)) {  //当目录不存在,就创建目录
  mkdir($path,0777,true);
  chmod($path, 0777);
}
//$destination=$path."/".$filename;
//得到唯一的文件名!防止因为文件名相同而产生覆盖
$uniName=md5(uniqid(microtime(true),true)).$ext;//md5加密,uniqid产生唯一id,microtime做前缀


if ($error==0) {
  if ($size>$maxsize) {
    exit("上传文件过大!");
  }
  if (!in_array($ext, $allowExt)) {
    exit("非法文件类型");
  }
  if (!is_uploaded_file($tmp_name)) {
    exit("上传方式有误,请使用post方式");
  }
  if (@move_uploaded_file($tmp_name, $uniName)) {//@错误抑制符,不让用户看到警告
    echo "文件".$filename."上传成功!";
  }else{
    echo "文件".$filename."上传失败!";
  }
  //判断是否为真实图片(防止伪装成图片的病毒一类的
  if (!getimagesize($tmp_name)) {//getimagesize真实返回数组,否则返回false
    exit("不是真正的图片类型");
  }

}else{
  switch ($error){
    case 1:
      echo "超过了上传文件的最大值,请上传2M以下文件";
      break;
    case 2:
      echo "上传文件过多,请一次上传20个及以下文件!";
      break;
    case 3:
      echo "文件并未完全上传,请再次尝试!";
      break;
    case 4:
      echo "未选择上传文件!";
      break;
    case 7:
      echo "没有临时文件夹";
      break;
  }
}
这里,具体实现都有注释,每一步其实都可以自己
登入後複製

#2.3 封裝

函數


<?php
function uploadFile($fileInfo,$path,$allowExt,$maxSize){

$filename=$fileInfo["name"];
$tmp_name=$fileInfo["tmp_name"];
$size=$fileInfo["size"];
$error=$fileInfo["error"];
$type=$fileInfo["type"];

//服务器端设定限制

$ext=pathinfo($filename,PATHINFO_EXTENSION);

//目的信息
if (!file_exists($path)) {  
  mkdir($path,0777,true);
  chmod($path, 0777);
}
$uniName=md5(uniqid(microtime(true),true)).&#39;.&#39;.$ext;
$destination=$path."/".$uniName;


if ($error==0) {
  if ($size>$maxSize) {
    exit("上传文件过大!");
  }
  if (!in_array($ext, $allowExt)) {
    exit("非法文件类型");
  }
  if (!is_uploaded_file($tmp_name)) {
    exit("上传方式有误,请使用post方式");
  }
  //判断是否为真实图片(防止伪装成图片的病毒一类的
  if (!getimagesize($tmp_name)) {//getimagesize真实返回数组,否则返回false
    exit("不是真正的图片类型");
  }
  if (@move_uploaded_file($tmp_name, $destination)) {//@错误抑制符,不让用户看到警告
    echo "文件".$filename."上传成功!";
  }else{
    echo "文件".$filename."上传失败!";
  }
  

}else{
  switch ($error){
    case 1:
      echo "超过了上传文件的最大值,请上传2M以下文件";
      break;
    case 2:
      echo "上传文件过多,请一次上传20个及以下文件!";
      break;
    case 3:
      echo "文件并未完全上传,请再次尝试!";
      break;
    case 4:
      echo "未选择上传文件!";
      break;
    case 7:
      echo "没有临时文件夹";
      break;
  }
}
return $destination;
}
登入後複製

呼叫


#
<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);
$fileInfo=$_FILES["myFile"];
$maxSize=10485760;//10M,10*1024*1024
$allowExt=array(&#39;jpeg&#39;,&#39;jpg&#39;,&#39;png&#39;,&#39;tif&#39;);
$path="uploads";
include_once &#39;upFunc.php&#39;;
uploadFile($fileInfo, $path, $allowExt, $maxSize);
登入後複製

三、多檔案的上傳實作

3.1 利用單一檔案封裝


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="doAction5.php" method="post" enctype="multipart/form-data">
请选择您要上传的文件:<input type="file" name="myFile1" /><br/>
请选择您要上传的文件:<input type="file" name="myFile2" /><br/>
请选择您要上传的文件:<input type="file" name="myFile3" /><br/>
请选择您要上传的文件:<input type="file" name="myFile4" /><br/>
<input type="submit" value="上传"/>
</form>
</body>
</html>
<?php
//print_r($_FILES);
header(&#39;content-type:text/html;charset=utf-8&#39;);
include_once &#39;upFunc.php&#39;;
foreach ($_FILES as $fileInfo){
  $file[]=uploadFile($fileInfo);
}
登入後複製

這裡的思路,從print_r($_FILES)去找,列印出來看到是個二維數組,很簡單,遍歷去用就好了!

上面那個function的定義改一下,給定一些默認值


function uploadFile($fileInfo,$path="uploads",$allowExt=array(&#39;jpeg&#39;,&#39;jpg&#39;,&#39;png&#39;,&#39;tif&#39;),$maxSize=10485760){
登入後複製

這樣子,簡單是簡單,但遇到一些問題。

正常的上傳4個圖片是沒問題,但要是中間激活了函數中的exit,就會立即停止,導致其他圖片也無法上傳。

3.2 升級版封裝

旨在實現針對多個或單一文件上傳的封裝

首先這樣子寫個靜態文件


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="doAction5.php" method="post" enctype="multipart/form-data">
请选择您要上传的文件:<input type="file" name="myFile[]" /><br/>
请选择您要上传的文件:<input type="file" name="myFile[]" /><br/>
请选择您要上传的文件:<input type="file" name="myFile[]" /><br/>
请选择您要上传的文件:<input type="file" name="myFile[]" /><br/>
<input type="submit" value="上传"/>
</form>
</body>
</html>
登入後複製

列印一下$_FILES

Array
(
  [myFile] => Array
    (
      [name] => Array
        (
          [0] => test32.png
          [1] => test32.png
          [2] => 333.png
          [3] => test41.png
        )

      [type] => Array
        (
          [0] => image/png
          [1] => image/png
          [2] => image/png
          [3] => image/png
        )

      [tmp_name] => Array
        (
          [0] => D:\wamp\tmp\php831C.tmp
          [1] => D:\wamp\tmp\php834C.tmp
          [2] => D:\wamp\tmp\php837C.tmp
          [3] => D:\wamp\tmp\php83BB.tmp
        )

      [error] => Array
        (
          [0] => 0
          [1] => 0
          [2] => 0
          [3] => 0
        )

      [size] => Array
        (
          [0] => 46174
          [1] => 46174
          [2] => 34196
          [3] => 38514
        )

    )

)
登入後複製

可以得到一个三维数组。

复杂是复杂了,但复杂的有规律,各项数值都在一起了,很方便我们取值!!

所以先得到文件信息,变成单文件处理那种信息

function getFiles(){
  $i=0;
  foreach($_FILES as $file){
    if(is_string($file[&#39;name&#39;])){ //单文件判定
      $files[$i]=$file;
      $i++;
    }elseif(is_array($file[&#39;name&#39;])){
      foreach($file[&#39;name&#39;] as $key=>$val){ //我的天,这个$key用的diao
        $files[$i][&#39;name&#39;]=$file[&#39;name&#39;][$key];
        $files[$i][&#39;type&#39;]=$file[&#39;type&#39;][$key];
        $files[$i][&#39;tmp_name&#39;]=$file[&#39;tmp_name&#39;][$key];
        $files[$i][&#39;error&#39;]=$file[&#39;error&#39;][$key];
        $files[$i][&#39;size&#39;]=$file[&#39;size&#39;][$key];
        $i++;
      }
    }
  }
  return $files;
  
}
登入後複製

然后之前的那种exit错误,就把exit改一下就好了,这里用res

function uploadFile($fileInfo,$path=&#39;./uploads&#39;,$flag=true,$maxSize=1048576,$allowExt=array(&#39;jpeg&#39;,&#39;jpg&#39;,&#39;png&#39;,&#39;gif&#39;)){
  //$flag=true;
  //$allowExt=array(&#39;jpeg&#39;,&#39;jpg&#39;,&#39;gif&#39;,&#39;png&#39;);
  //$maxSize=1048576;//1M
  //判断错误号
  $res=array();
  if($fileInfo[&#39;error&#39;]===UPLOAD_ERR_OK){
    //检测上传得到小
    if($fileInfo[&#39;size&#39;]>$maxSize){
      $res[&#39;mes&#39;]=$fileInfo[&#39;name&#39;].&#39;上传文件过大&#39;;
    }
    $ext=getExt($fileInfo[&#39;name&#39;]);
    //检测上传文件的文件类型
    if(!in_array($ext,$allowExt)){
      $res[&#39;mes&#39;]=$fileInfo[&#39;name&#39;].&#39;非法文件类型&#39;;
    }
    //检测是否是真实的图片类型
    if($flag){
      if(!getimagesize($fileInfo[&#39;tmp_name&#39;])){
        $res[&#39;mes&#39;]=$fileInfo[&#39;name&#39;].&#39;不是真实图片类型&#39;;
      }
    }
    //检测文件是否是通过HTTP POST上传上来的
    if(!is_uploaded_file($fileInfo[&#39;tmp_name&#39;])){
      $res[&#39;mes&#39;]=$fileInfo[&#39;name&#39;].&#39;文件不是通过HTTP POST方式上传上来的&#39;;
    }
    if($res) return $res;
    //$path=&#39;./uploads&#39;;
    if(!file_exists($path)){
      mkdir($path,0777,true);
      chmod($path,0777);
    }
    $uniName=getUniName();
    $destination=$path.&#39;/&#39;.$uniName.&#39;.&#39;.$ext;
    if(!move_uploaded_file($fileInfo[&#39;tmp_name&#39;],$destination)){
      $res[&#39;mes&#39;]=$fileInfo[&#39;name&#39;].&#39;文件移动失败&#39;;
    }
    $res[&#39;mes&#39;]=$fileInfo[&#39;name&#39;].&#39;上传成功&#39;;
    $res[&#39;dest&#39;]=$destination;
    return $res;
    
  }else{
    //匹配错误信息
    switch ($fileInfo [&#39;error&#39;]) {
      case 1 :
        $res[&#39;mes&#39;] = &#39;上传文件超过了PHP配置文件中upload_max_filesize选项的值&#39;;
        break;
      case 2 :
        $res[&#39;mes&#39;] = &#39;超过了表单MAX_FILE_SIZE限制的大小&#39;;
        break;
      case 3 :
        $res[&#39;mes&#39;] = &#39;文件部分被上传&#39;;
        break;
      case 4 :
        $res[&#39;mes&#39;] = &#39;没有选择上传文件&#39;;
        break;
      case 6 :
        $res[&#39;mes&#39;] = &#39;没有找到临时目录&#39;;
        break;
      case 7 :
      case 8 :
        $res[&#39;mes&#39;] = &#39;系统错误&#39;;
        break;
    }
    return $res;
  }
}
登入後複製

里面封装了两个小的

function getExt($filename){
  return strtolower(pathinfo($filename,PATHINFO_EXTENSION));
}

/**
 * 产生唯一字符串
 * @return string
 */
function getUniName(){
  return md5(uniqid(microtime(true),true));
}
登入後複製

然后静态中,用multiple属性实现多个文件的输入;

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="doAction6.php" method="POST" enctype="multipart/form-data">
请选择您要上传的文件:<input type="file" name="myFile[]" multiple=&#39;multiple&#39; /><br/>
<input type="submit" value="上传"/>
</form>
</body>
</html>
doAction6
<?php 
//print_r($_FILES);
header("content-type:text/html;charset=utf-8");
require_once &#39;upFunc2.php&#39;;
require_once &#39;common.func.php&#39;;
$files=getFiles();
// print_r($files);
foreach($files as $fileInfo){
  $res=uploadFile($fileInfo);
  echo $res[&#39;mes&#39;],&#39;<br/>&#39;;
  $uploadFiles[]=@$res[&#39;dest&#39;];
}
$uploadFiles=array_values(array_filter($uploadFiles));
//print_r($uploadFiles);
登入後複製

这样子的几个文件,就实现比较强大的面向过程的上传文件的功能(学的叫一个心酸。。。);

四、面向对象的文件上传

<?php 
class upload{
  protected $fileName;
  protected $maxSize;
  protected $allowMime;
  protected $allowExt;
  protected $uploadPath;
  protected $imgFlag;
  protected $fileInfo;
  protected $error;
  protected $ext;
  /**
   * @param string $fileName
   * @param string $uploadPath
   * @param string $imgFlag
   * @param number $maxSize
   * @param array $allowExt
   * @param array $allowMime
   */
  public function __construct($fileName=&#39;myFile&#39;,$uploadPath=&#39;./uploads&#39;,$imgFlag=true,$maxSize=5242880,$allowExt=array(&#39;jpeg&#39;,&#39;jpg&#39;,&#39;png&#39;,&#39;gif&#39;),$allowMime=array(&#39;image/jpeg&#39;,&#39;image/png&#39;,&#39;image/gif&#39;)){
    $this->fileName=$fileName;
    $this->maxSize=$maxSize;
    $this->allowMime=$allowMime;
    $this->allowExt=$allowExt;
    $this->uploadPath=$uploadPath;
    $this->imgFlag=$imgFlag;
    $this->fileInfo=$_FILES[$this->fileName];
  }
  /**
   * 检测上传文件是否出错
   * @return boolean
   */
  protected function checkError(){
    if(!is_null($this->fileInfo)){
      if($this->fileInfo[&#39;error&#39;]>0){
        switch($this->fileInfo[&#39;error&#39;]){
          case 1:
            $this->error=&#39;超过了PHP配置文件中upload_max_filesize选项的值&#39;;
            break;
          case 2:
            $this->error=&#39;超过了表单中MAX_FILE_SIZE设置的值&#39;;
            break;
          case 3:
            $this->error=&#39;文件部分被上传&#39;;
            break;
          case 4:
            $this->error=&#39;没有选择上传文件&#39;;
            break;
          case 6:
            $this->error=&#39;没有找到临时目录&#39;;
            break;
          case 7:
            $this->error=&#39;文件不可写&#39;;
            break;
          case 8:
            $this->error=&#39;由于PHP的扩展程序中断文件上传&#39;;
            break;
            
        }
        return false;
      }else{
        return true;
      }
    }else{
      $this->error=&#39;文件上传出错&#39;;
      return false;
    }
  }
  /**
   * 检测上传文件的大小
   * @return boolean
   */
  protected function checkSize(){
    if($this->fileInfo[&#39;size&#39;]>$this->maxSize){
      $this->error=&#39;上传文件过大&#39;;
      return false;
    }
    return true;
  }
  /**
   * 检测扩展名
   * @return boolean
   */
  protected function checkExt(){
    $this->ext=strtolower(pathinfo($this->fileInfo[&#39;name&#39;],PATHINFO_EXTENSION));
    if(!in_array($this->ext,$this->allowExt)){
      $this->error=&#39;不允许的扩展名&#39;;
      return false;
    }
    return true;
  }
  /**
   * 检测文件的类型
   * @return boolean
   */
  protected function checkMime(){
    if(!in_array($this->fileInfo[&#39;type&#39;],$this->allowMime)){
      $this->error=&#39;不允许的文件类型&#39;;
      return false;
    }
    return true;
  }
  /**
   * 检测是否是真实图片
   * @return boolean
   */
  protected function checkTrueImg(){
    if($this->imgFlag){
      if(!@getimagesize($this->fileInfo[&#39;tmp_name&#39;])){
        $this->error=&#39;不是真实图片&#39;;
        return false;
      }
      return true;
    }
  }
  /**
   * 检测是否通过HTTP POST方式上传上来的
   * @return boolean
   */
  protected function checkHTTPPost(){
    if(!is_uploaded_file($this->fileInfo[&#39;tmp_name&#39;])){
      $this->error=&#39;文件不是通过HTTP POST方式上传上来的&#39;;
      return false;
    }
    return true;
  }
  /**
   *显示错误 
   */
  protected function showError(){
    exit(&#39;<span style="color:red">&#39;.$this->error.&#39;</span>&#39;);
  }
  /**
   * 检测目录不存在则创建
   */
  protected function checkUploadPath(){
    if(!file_exists($this->uploadPath)){
      mkdir($this->uploadPath,0777,true);
    }
  }
  /**
   * 产生唯一字符串
   * @return string
   */
  protected function getUniName(){
    return md5(uniqid(microtime(true),true));
  }
  /**
   * 上传文件
   * @return string
   */
  public function uploadFile(){
    if($this->checkError()&&$this->checkSize()&&$this->checkExt()&&$this->checkMime()&&$this->checkTrueImg()&&$this->checkHTTPPost()){
      $this->checkUploadPath();
      $this->uniName=$this->getUniName();
      $this->destination=$this->uploadPath.&#39;/&#39;.$this->uniName.&#39;.&#39;.$this->ext;
      if(@move_uploaded_file($this->fileInfo[&#39;tmp_name&#39;], $this->destination)){
        return $this->destination;
      }else{
        $this->error=&#39;文件移动失败&#39;;
        $this->showError();
      }
    }else{
      $this->showError();
    }
  }
}
<?php 
header(&#39;content-type:text/html;charset=utf-8&#39;);
require_once &#39;upload.class.php&#39;;
$upload=new upload(&#39;myFile1&#39;,&#39;imooc&#39;);
$dest=$upload->uploadFile();
echo $dest;
登入後複製

四、下载

对于浏览器不识别的,可以直接下载,但对于能识别的,需要多一两步

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Insert title here</title>
</head>
<body>
<a href="1.rar">下载1.rar</a>
<br />
<a href="1.jpg">下载1.jpg</a>
<br />
<a href="doDownload.php?filename=1.jpg">通过程序下载1.jpg</a>
<br />
<a href="doDownload.php?filename=../upload/nv.jpg">下载nv.jpg</a>
<?php

?>
</body>
</html>
<?php 
$filename=$_GET[&#39;filename&#39;];
header(&#39;content-disposition:attachment;filename=&#39;.basename($filename));
header(&#39;content-length:&#39;.filesize($filename));
readfile($filename);
登入後複製

总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。

相关推荐:

PHP字符串定义方式及各自区别 

php substr函数定义与用法汇总

php 三元运算符实例详细介绍

以上是PHP實作檔案上傳下載的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!