欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php文件上傳的兩種實(shí)現(xiàn)方法

 更新時(shí)間:2016年04月04日 18:42:06   作者:飛鴻影~  
這篇文章主要為大家詳細(xì)介紹了兩種php文件上傳的實(shí)現(xiàn)方法,感興趣的朋友可以參考一下

文件上傳一般有下面2種方式:

有兩種:
1、標(biāo)準(zhǔn)input表單方式,典型的用$_FILES進(jìn)行接收;
2、以Base64的方式進(jìn)行傳送,一般是AJAX異步上傳。

第一種
標(biāo)準(zhǔn)的input表單方式,適用于大文件進(jìn)行上傳,同時(shí)支持批量。html代碼關(guān)鍵的幾句:

<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時(shí):

<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"對(duì)于文件上傳是必不可少的。另外type="file"設(shè)置input類(lèi)型,accept="image/*"指定優(yōu)先上傳圖片(MIME 參考手冊(cè))。multiple支持一次選多個(gè)文件,pic[]以數(shù)組的形式接收多個(gè)文件。手機(jī)端端還可以加入?yún)?shù)capture="camera"選擇攝像頭拍照上傳。

后端處理:
通過(guò)$_FILES獲取上傳的文件。

$files = $_FILES;
傳多個(gè)文件時(shí),如果name不同,則返回的$_FILES數(shù)組格式不同。

name相同時(shí):

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不相同時(shí):

   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)
 }
}

在對(duì)$_FILES進(jìn)行foreach遍歷時(shí),前面那種輸出格式不大方便。后面那種就可以直接遍歷。我們可以寫(xiě)個(gè)方法進(jìn)行統(tǒng)一轉(zhuǎn)換:

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

好,前面講到后端如何處理接收到的$_FILES數(shù)組,并轉(zhuǎn)換成統(tǒng)一格式。接下來(lái)任務(wù)主要是:
1、檢測(cè)上傳的文件是否非法;
2、檢測(cè)上傳的文件是否超過(guò)大??;
3、檢測(cè)保存的路徑是否存在,是否可寫(xiě);
4、文件重命名;

其中上傳過(guò)程中用到了個(gè)很重要的函數(shù):move_uploaded_file(filename , $destination )進(jìn)行文件移動(dòng)操作。將$_FILES['id_pic']['tmp_name']移動(dòng)到新的路徑里。當(dāng)然,移動(dòng)前可以用is_uploaded_file($_FILES['id_pic']['tmp_name'])進(jìn)行判斷文件是否正常上傳的。

多文件上傳則是循環(huán)的方法多次使用move_uploaded_file()進(jìn)行移動(dòng)操作。

第二種
主要以上傳圖片為主。
利用input的change事件,借助canvas對(duì)圖片進(jìn)行處理(例如壓縮),然后ajax發(fā)送文件流到后端。

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

后端處理:
后端最終會(huì)收到前端發(fā)送的base64字符串,接著處理字符串為圖片即可。具體請(qǐng)使用關(guān)鍵字base64 轉(zhuǎn) image 開(kāi)發(fā)語(yǔ)言進(jìn)行谷歌|百度。前端生成的結(jié)果中有一個(gè)base64Len,這是字符串的長(zhǎng)度,后端應(yīng)該核對(duì)以確認(rèn)是否提交完整。

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。

相關(guān)文章

最新評(píng)論