首頁  >  文章  >  後端開發  >  php實作檔案上傳的兩種方法

php實作檔案上傳的兩種方法

墨辰丷
墨辰丷原創
2018-06-04 10:25:112079瀏覽

這篇文章主要為大家詳細介紹了兩種php檔案上傳的實作方法,有興趣的朋友可以參考一下

檔案上傳一般有下面2種方式:

有兩種:
1、標準input表單方式,典型的用$_FILES進行接收;
2、以Base64的方式進行傳送,通常是AJAX非同步上傳。

第一種
標準的input表單方式,適用於大檔案進行上傳,同時支援批次。 html程式碼關鍵的幾句:

<form enctype="multipart/form-data" method="post" action="upload.php"">
  <input type="file" name="id_pic[]" accept="image/*" class="form-control" multiple />
  <input type="submit" value="上传 " />
</form>

不同的name時:

<form enctype="multipart/form-data" method="post" action="upload.php"">
  <input type="file" name="id_pic_1" accept="image/*" class="form-control" />
  <input type="file" name="id_pic_2" accept="image/*" class="form-control" />
  <input type="submit" value="上传 " />
</form>

其中enctype="multipart/form-data"對於檔案上傳是不可或缺的。另外type="file"設定input類型,accept="image/*"指定優先上傳圖片(MIME 參考手冊)。 multiple支援一次選取多個文件,pic[]以陣列的形式接收多個文件。手機端端也可以加入參數capture="camera"選擇相機拍照上傳。

後端處理:
透過$_FILES取得上傳的檔案。

$files = $_FILES;
傳多個檔案時,如果name不同,則傳回的$_FILES陣列格式不同。

name相同時:

array(1) {
 ["id_pic"] => array(5) {
  ["name"] => array(2) {
   [0] => string(5) "1.jpg"
   [1] => string(5) "2.jpg"
  }
  ["type"] => array(2) {
   [0] => string(10) "image/jpeg"
   [1] => string(10) "image/jpeg"
  }
  ["tmp_name"] => array(2) {
   [0] => string(27) "C:\Windows\Temp\php7A7E.tmp"
   [1] => string(27) "C:\Windows\Temp\php7A7F.tmp"
  }
  ["error"] => array(2) {
   [0] => int(0)
   [1] => int(0)
  }
  ["size"] => array(2) {
   [0] => int(77357)
   [1] => int(56720)
  }
 }
}

name不相同時:

   array(2) {
 ["id_pic_1"] => array(5) {
  ["name"] => string(5) "1.jpg"
  ["type"] => string(10) "image/jpeg"
  ["tmp_name"] => string(27) "C:\Windows\Temp\phpBBEE.tmp"
  ["error"] => int(0)
  ["size"] => int(77357)
 }
 ["id_pic_2"] => array(5) {
  ["name"] => string(5) "2.jpg"
  ["type"] => string(10) "image/jpeg"
  ["tmp_name"] => string(27) "C:\Windows\Temp\phpBBEF.tmp"
  ["error"] => int(0)
  ["size"] => int(56720)
 }
}

在對$ _FILES進行foreach遍歷時,前面那種輸出格式不大方便。後面那種就可以直接遍歷。我們可以寫個方法進行統一轉換:

function dealFiles($files) {
    $fileArray = array();
    $n     = 0;
    foreach ($files as $key=>$file){
      if(is_array($file[&#39;name&#39;])) {
        $keys    =  array_keys($file);
        $count   =  count($file[&#39;name&#39;]);
        for ($i=0; $i<$count; $i++) {
          $fileArray[$n][&#39;key&#39;] = $key;
          foreach ($keys as $_key){
            $fileArray[$n][$_key] = $file[$_key][$i];
          }
          $n++;
        }
      }else{
        $fileArray = $files;
        break;
      }
    }
    return $fileArray;
 }

好,前面講到後端如何處理接收到的$_FILES數組,並轉換成統一格式。接下來任務主要是:
1、偵測上傳的檔案是否非法;
2、偵測上傳的檔案是否超過大小;
3、偵測已儲存的路徑是否存在,是否可寫入;
4.檔案重新命名;

其中上傳過程中用到了個很重要的函數:move_uploaded_file(filename , $destination )進行檔案移動操作。將$_FILES['id_pic']['tmp_name']移到新的路徑裡。當然,移動前可以用is_uploaded_file($_FILES['id_pic']['tmp_name'])進行判斷檔案是否正常上傳的。

多重檔案上傳則是循環的方法多次使用move_uploaded_file()進行移動操作。

第二種
主要以上傳圖片為主。
利用input的change事件,借助canvas對圖片進行處理(例如壓縮),然後ajax發送檔案流到後端。

基本原理是透過canvas渲染圖片,再透過 toDataURL 方法壓縮儲存為base64字串(能夠編譯成jpg格式的圖片)。

後端處理:
後端最終會收到前端發送的base64字串,接著處理字串為圖片即可。具體請使用關鍵字base64 轉 image 開發語言進行Google|百度。前端產生的結果中有一個base64Len,這是字串的長度,後端應該檢查以確認是否提交完整。

//php示例:
$img = base64_decode($_POST[&#39;img&#39;]);
$img = imagecreatefromstring($img);

總結:以上就是這篇文章的全部內容,希望能對大家的學習有所幫助。

相關推薦:

php連接oracle資料庫的方法及簡單分析

PHP封裝的MSSql操作類別以及完整實例分析

php連接oracle資料庫的核心步驟及簡要分析

##

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

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn