"PHP additions and Deletions Instance" section 23rd-php file Upload

Source: Internet
Author: User
Tags php file upload

22.1. php File Upload resource file


Copy the three items to the root directory of the project.

When the copy is complete, open the upload.html:


Now, we're going to write a upload.php at the root of the project.

PHP provides us with a lot of predefined variables for file uploads, such as file names, file sizes, file types, and so on.

The following up_file is the name of the file box in the form form

22.2 How to modify the file upload size limit open the XAMPP installation directory and search the directory for the php.ini configuration file.


Open php.ini, search Upload_max:

Re-search Post_max:

Next, restart the server.

22.3 changing the name of a file size
    function getSize($fileSize){        if($fileSize < 1024){            $fileSize = $fileSize . "B";        }else if($fileSize < 1024 * 1024){            $fileSize = round($fileSize / 1024,2) . "KB";        }else if($fileSize < 1024 * 1024 * 1024){            $fileSize = round($fileSize / 1024 / 1024,2) . "MB";        }else if($fileSize < 1024 * 1024 * 1024 * 1024){            $fileSize = round($fileSize / 1024 / 1024 / 1024 , 2) . "GB";        }        return $fileSize;    }
22.4 File Upload

In PHP, the file upload will default upload to a temporary directory, after uploading, if not in time to copy the uploaded files from the temporary directory to the uploaded folder, temporary files will be deleted.

So, when the file upload is successful, we must be in a timely manner to the temporary copy to the upload directory.

    $temp = $_FILES["up_file"]["tmp_name"];    move_uploaded_file($temp,"upload/" . time() . "_" .  $_FILES["up_file"]["name"]);    echo "上传成功!";

At the same time, in order to solve the problem of overwriting files with the same name, we'd better change the name of the uploaded filename and add a timestamp before the file name.

If the uploaded file is a picture, then, in the upload successful page, the uploaded image is displayed on the page, let the user know what picture they upload.

$des = "upload/" . time() . "_" .  $_FILES["up_file"]["name"];    $temp = $_FILES["up_file"]["tmp_name"];    move_uploaded_file($temp,$des);    echo "上传成功!<br>";//先判断是不是图片?    if(strstr($_FILES["up_file"]["type"], "image")){        //如果是图片的话,就把上传好的图片显示在页面上        echo "<image src=‘".$des."‘ width=‘360px‘ />";    }

SOURCE Access: HTTPS://WWW.JIANSHU.COM/P/4977BD0073D5

"PHP additions and Deletions Instance" section 23rd-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.