[Moka Classmate Code] PHP basic knowledge: Upload File source code

Source: Internet
Author: User

1. Directory structure

2.index.php

= "File" > File name </label>    <input type= "file" name= "file" id= "file" ><br>    <input type= "Submit" value= " Submit "></form></body>

3.upload.php

<?PHP//upload file code//core code:/*if ($_files[' file ' [' Error ']>0) {echo ' errors: '. $_files[' file '] [' ERROR ']. ' <br> ';} else{echo ' upload file name: '. $_files[' file ' [' name ']. '    <br> '; echo ' filename type: '. $_files[' file ' [' type ']. '    <br> '; echo ' File size: '. ($_files[' file ' [' Size ']/1024). '    Kb<br> '; echo ' Files temporary storage address: '. $_files[' file ' [' Tem_name '];}*///the code required to upload the file, but before uploading, you need to determine the file limit, size, type, etc.//allow Shanghai to flee the picture suffix$allowedExts=Array("GIF", "JPEG", "JPG", "PNG");$temp=Explode(".",$_files["File"] ["name"]);//intercept file suffix$extension=End($temp);//file suffix$type=$_files["File"] ["Type"];$size=$_files["File"] ["Size"];$error=$_files["File"] ["Error"];$name=$_files["File"] ["Name"];$tmp _name=$_files["File"] ["Tmp_name"];if(($type= = "Image/gif") | | ($type= = "Image/jpeg") | | ($type= = "Image/jpg") | | ($type= = "Image/pjpeg") | |    ($type= = "Image/x-png") | | ($type= = "Image/png") &&$size< 2048 &&In_array($extension,$allowedExts)) {    if($error> 0) {        Echo"Error:".$error. ' <br> '; } Else {        Echo"File name:".$name. "<br>"; Echo"File type:".$type. "<br>"; Echo"File Size:". ($size/1024). "Kb<br>"; Echo"Temporary storage location:".$tmp _name; //determine whether the file exists in the directory where the file is stored, and if not, you need to create it, file's directory permission is 777        if(file_exists("file/".$name)) {            Echo $name. "The file already exists. "; } Else {            Move_uploaded_file($tmp _name, ' file/'.$name); Echo"File is stored in:". "file/".$name; }    }} Else {    Echo"Illegal Files";}

[Moka Classmate Code] PHP basic knowledge: Upload File source code

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.