About PHP HTML <input type= "file" Name= "img"/> Upload image, image size, width height, suffix name.

Source: Internet
Author: User
Tags explode

In our system, it is unavoidable to upload pictures, videos and other documents, in the cross, need to do some judgments, file size and so on.

Attention:

In the php.ini in the post_max_size,upload_max_filesize default is 2M, when uploading the video, you need to modify, you can set your own.

In addition, if memory limits are enabled, the value should be less than the value of the Memory_limit option.

When uploading the video, it can take some time, when more than a certain time, will report the script to execute more than 30 seconds of error, because in the php.ini configuration file max_execution_time configuration options in mischief, which represents the maximum allowable execution time per script (seconds), 0 Indicates no limit. You can adjust the value of the max_execution_time appropriately, and it is not recommended to set to 0.

Front Page: HTML

1 <formenctype= "Multipart/form-data"Action= ' text.php 'Method= "POST">2     <inputtype= "File"name= "Upfile">3 4     <inputtype= "Submit"value= ' upload file '>5 </form>

Background page: PHP processing

1<script src= "./js/jquery2.0.3.min.js" ></script>2<?PHP3 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) {Ten //echo "picture length or width exceeds limit"; One //exit; A //} - /*if ($_files[' upfile ' [' Size ']>20*1024*1024) { - echo "picture too big"; the }*/ - include_once' Common/util.php '; -Getimgw_h ($_files[' Upfile '],10,2016,40, "apk");

3.common files Write your own function: util.php

/** * @param $file _tmpname * Limit the width of the uploaded file, size, suffix * $file = $_files[' Upfile '), $w maximum width, $h maximum height, $size maximum file size (in kilobytes), $type Prefix name*/functionGetimgw_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 ("Please enter a suffix named <?php Echo$type;?> 's File "); Window. History.go (-1); </script> <?PHPExit; }    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 ("The picture is longer or wider than the limit, wide <?php echo$w; <?php, High-echo$h;?> "); Window. History.go (-1); </script> <?PHPExit; }    }    if($file[' Size ']>$size*1024){        ?> <script type= "Text/javascript" >Alert ("The picture is too large, not greater than <?php Echo$size;? >kb "); Window. History.go (-1); </script> <?PHPExit; }}

About PHP HTML <input type= "file" Name= "img"/> Upload image, image size, width height, suffix name.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.