PHP implementation File Upload

Source: Internet
Author: User

This is the PHP implementation of File upload example

PHPDefine("Max_file_size", 102400);//definition file max_file_size$upfile=$_files["Upfile"];//gets the value in the array$name=$upfile["Name"];//file name$type=$upfile["type"];//File Type$size=$upfile["Size"];//File Size$tmp _name=$upfile["Tmp_name"];//Temporary file pathif(isset($_post["Sub"])){if(Is_uploaded_file($tmp _name)){//determine if the upload file existsSwitch($type){//Switch Judgment Type Case"Image/png":$oktype=true; Break; Case"Image/jpeg":$oktype=true; Break; Case"Image/gif":$oktype=true; Break; Case"Image/jpg":$oktype=true; Break; Case"Image/bmp":$oktype=true; Break;default:$oktype=true;}if($oktype){//if it returns True$error=$upfile["Error"];//the value returned by the system after uploading/** * 0. Upload success * 1. exceeds file size, php.ini specified value * 2. File size exceeded, Max_file_size * 3. File partial upload * 4. File upload failed * 5. uploaded file size is 0*/ if($error==0&&$size<=max_file_size) {//file size less than max_file_sizeEcho"<br> Upload Success!<br>";//foreach ($upload as $name = + $value) {//echo $name. " = ". $value;//}Echo"File name:".$name." <br> ";//Output File nameEcho"File Size:".$size." <br> ";//Output File SizeEcho"File type:".$type." <br> ";//output File TypeEcho"File Temp file path:".$tmp _name." <br> ";//Output File Temp path$upload= "upload/";//file directoryif(!file_exists($upload)){//determine if the file directory existsmkdir($upload);//does not exist, the file directory is createdchmod($upload, "700");//read/write browse}$iconv=Iconv("Utf-8", "gb2312",$name);//Convert encoding Format$destination=$upload.$iconv;//PathMove_uploaded_file($tmp _name,$destination);//If the upload succeeds, move the file to the created directoryEcho"Picture Preview:<br>";Echo"$destination' ></img> ';//Previewing files}Else if($error==1) {//Type of errorEcho"Over file size, modified under PHP.ini file"; }Else if($error==2){Echo"More than the file size, set value of the max_file_size option";}Else if($error==3){Echo"File only partially uploaded";}Else if($error==4){Echo"File not uploaded";}Else if($error==5){Echo"The uploaded file size is zero";}Else{Echo"Please upload files in JPEG,GIF,PNH format";} }                                    }}?>

PHP implementation File Upload

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.