PHP File Upload

Source: Internet
Author: User

With PHP, you can upload files to the server.
Test project, the directory structure is:

Test
|-----Upload # File uploaded directory
|-----form.html # form File
|-----upload_file.php # PHP upload code


form.html # Form File

"Utf-8"><title> Rookie Tutorial (runoob.com) </title>"upload_file.php"Method="Post"Enctype="Multipart/form-data"> <label for="file"> FileName:</label> <input type="file"Name="file"Id="file"><br> <input type="Submit"Name="Submit"Value="Submit"></form></body>

upload_file.php # PHP upload code

<?PHP//allow uploading of picture suffixes$allowedExts = Array ("gif","JPEG","jpg","PNG"); $temp= Explode (".", $_files["file"]["name"]); Echo $_files["file"]["size"]."<br>"; $extension= End ($temp);//get file suffix nameif((($_files["file"]["type"] =="Image/gif")|| ($_files["file"]["type"] =="Image/jpeg")|| ($_files["file"]["type"] =="image/jpg")|| ($_files["file"]["type"] =="Image/pjpeg")|| ($_files["file"]["type"] =="Image/x-png")|| ($_files["file"]["type"] =="Image/png"))&& ($_files["file"]["size"] <204800)//Less than KB&&In_array ($extension, $allowedExts)) {    if($_files["file"]["Error"] >0) {echo"Error::". $_files["file"]["Error"] ."<br>"; }    Else{echo"upload file name:". $_files["file"]["name"] ."<br>"; Echo"file type: the". $_files["file"]["type"] ."<br>"; Echo"File Size:". ($_files["file"]["size"] /1024x768) ."kb<br>"; Echo"location of temporary storage of files:". $_files["file"]["Tmp_name"] ."<br>"; //determine if the file exists in the upload directory under the current directory//If there is no upload directory, you need to create it, upload directory permission is 777        if(File_exists ("upload/". $_files["file"]["name"]) {echo $_files["file"]["name"] ."file already exists. "; }        Else        {            //If the file does not exist in the upload directory, upload the file to the upload directoryMove_uploaded_file ($_files["file"]["Tmp_name"],"upload/". $_files["file"]["name"]); Echo"files are stored in:"."upload/". $_files["file"]["name"]; }    }}Else{echo"Illegal file formats";}?>

PHP 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.