關於PHP HTML <input type="file" name="img"/>上傳圖片,圖片大小,寬高,尾碼名。

來源:互聯網
上載者:User

標籤:style   blog   io   ar   color   os   sp   java   for   

在我們的系統中,不免要上傳圖片,視頻等檔案,在上傳中,需要做的一些判斷,檔案大小等方面。

注意:

在php.ini 中的post_max_size,upload_max_filesize預設為2M,在上傳視頻的時候,需要修改下,可以自行設定。

另外如果啟用了記憶體限制,那麼該值應當小於memory_limit選項的值。

在上傳視頻的時候,可以會需要花費些時間,當超過一定的時間,會報指令碼執行超過30秒的錯誤,這是因為在php.ini設定檔中max_execution_time配置選項在作怪,其表示每個指令碼最大允許執行時間(秒),0 表示沒有限制。你可以適當調整max_execution_time的值,不推薦設定為0。

 

前台頁面:html

1 <form enctype="multipart/form-data" action=‘text.php‘ method="post">2     <input type="file" name="upfile">3 4     <input type="submit" value=‘上傳檔案‘>5 </form>

後台頁面:php 處理

 1 <script src="./js/jquery2.0.3.min.js"></script> 2 <?php 3 var_dump($_FILES); 4 //$_FILES[‘upfile‘][‘tmp_name‘] 5 //var_dump($_POST); 6 /*$size = getimagesize($_FILES[‘upfile‘][‘tmp_name‘]); 7 $width = $size[0]; 8 $height = $size[1];*/ 9 //if($width>165 || $height>216){10 //    echo "圖片長或寬超出限制";11 //    exit;12 //}13 /*if($_FILES[‘upfile‘][‘size‘]>20*1024*1024){14     echo "圖片過大";15 }*/16 include_once ‘common/util.php‘;17 getImgW_H($_FILES[‘upfile‘],10,2016,40,"apk");

3.common檔案下 自己寫的函數:util.php

/** * @param $file_tmpname * 限制上傳檔案 的 寬高,大小,尾碼名 * $file = $_files[‘upfile‘],$w 最大寬度,$h 最大高度,$size 最大檔案 大小(單位為kb),$type 尾碼名 */function getImgW_H($file,$w,$h,$size,$type){    $imgFileName = explode(".",$file[‘name‘]);    $imgExt = $imgFileName[count($imgFileName)-1];    if(!in_array($imgExt,explode(‘,‘,$type))){        ?>        <script type="text/javascript">            alert("請輸入尾碼名為<?php echo $type; ?>的檔案");            window.history.go(-1);        </script>        <?php        exit;    }    if(!empty($w)&&!empty($h)){        $s = getimagesize($file[‘tmp_name‘]);        $width = $s[0];        $height = $s[1];        if($width>$w || $height>$h){            ?>            <script type="text/javascript">                alert("圖片長或寬超出限制,寬<?php echo $w; ?>,高<?php echo $h; ?>");                window.history.go(-1);            </script>            <?php            exit;        }    }    if($file[‘size‘]>$size*1024){        ?>        <script type="text/javascript">            alert("圖片過大,不大於<?php echo $size; ?>kb");            window.history.go(-1);        </script>        <?php        exit;    }}

 

關於PHP HTML <input type="file" name="img"/>上傳圖片,圖片大小,寬高,尾碼名。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.