PHP中多張圖片上傳並校正的實現

來源:互聯網
上載者:User

單張的圖片上傳是不複雜的,這裡涉及到多張圖片上傳和對圖片格式的校正,保證上傳的一定是圖片,防止上傳其他檔案到伺服器。

基本實現演算法是使用數組的形式,把所有的圖片提交個一個數組,對數組的元素進行一個個的處理。

<?php
/*********************************************
 *  檔案:uploadimg.php
 *  用途:圖片上傳程式
 *  版本:v1.0
 *  建立:2005-03-28 11:07
 *  修改:2005-04-06 08:43
 *  著作權:heiyeluren
 *********************************************/

// 圖片目錄
$img_dir = "../upload/";
// …… html 顯示上傳介面

/* 圖片上傳處理 */
// 把圖片傳到伺服器
// 初始設定變數

$uploaded = 0;
$unuploaded = 0;

//只允許五張圖片上傳

for ($i=0; $i<=5; $i++)
{
       //擷取當前圖片的資訊
$is_file = $_FILES['imgfile']['name'][$i];
//如果當前圖片不為空白
       if (!empty($is_file))
       {
              //把當前圖片的資訊儲存到變數裡
              $result[$i] = "
                            <tr class=td2 align=center>
                            <td>". $_FILES['imgfile']['name'][$i] ."</td>
                            <td>". round($_FILES['imgfile']['size'][$i]/1024, 2) ."K</td>
                            <td>". $_FILES['imgfile']['type'][$i] ."</td>
                            <td>";

              // 判斷上傳的圖片的類型是不是jpg,gif,png,bmp中的一種,同時判斷是否上傳成功
              if (

                     $_FILES['imgfile']['type'][$i] == "image/pjpeg"   ||
                     $_FILES['imgfile']['type'][$i] == "image/gif"     ||
                     $_FILES['imgfile']['type'][$i] == "image/x-png"   ||
                     $_FILES['imgfile']['type'][$i] == "image/bmp"
                 )
              {
                     //如果上傳的檔案沒有在伺服器上存在
                     if (!file_exists($img_dir . $_FILES['imgfile']['name'][$i]))
                     {
                            //把圖片檔案從臨時檔案夾中轉移到我們指定上傳的目錄中
                            move_uploaded_file($_FILES['imgfile']['tmp_name'][$i],
      $img_dir . $_FILES['imgfile']['name'][$i]);
                            $result[$i] .= "成功";
                            $uploaded++;
                     }
                     else         //如果檔案已經在伺服器上存在
                     {
                            $result[$i] .= "<font color=red>檔案已存在</font>";
                            $unuploaded++;
                            continue;
                     }
              }
              else
              {
                     $result[$i] .= "<font color=red>失敗</font>";
                     $unuploaded++;
              }
              $result[$i] .= "</td></tr>";
       } //end if
} // end for
 

// 如果沒有選擇任何圖片
if (empty($result))
{
       prompt_msg("錯誤資訊", "沒有選擇任何圖片。", "返回上一步", "uploadimg.php?action=upload" );
       exit();
}

// 顯示所有上傳後的結果
echo " <table cellpadding=4 cellspacing=1 border=0 class=table width=400 align=left>
       <tr  class=navi align=center>
          <td>檔案名稱</td>
          <td>大小</td>
          <td>類型</td>
          <td>上傳結果</td>
       </tr>
       ";

foreach( $result as $value)
{
       echo $value;
}

echo "<tr  class=td1>
      <td colspan=4>共上傳 " . ($uploaded + $unuploaded) . ", 成功: $uploaded, 失敗:<font color=red> $unuploaded</font> </td>
      </tr>
      <tr class=navi>
       <td colspan=4 align=center>[ <a href='uploadimg.php?action=upload' title='繼續上傳'>繼續上傳</a> ] &nbsp;&nbsp;&nbsp;&nbsp;[ <a href='image.php' title='瀏覽圖片'>瀏覽圖片</a> ]</td>
     </tr>
   </table>
       ";
?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.