How to redefine an instance of an uploaded file name

Source: Internet
Author: User
Tags file handling
When uploading a file, you need to specify the path of the upload file, however, if the face of a large number of users upload different files, it will inevitably have the same name, which will cause file replacement, resulting in the loss of files, so it is very important to redefine the names of the uploaded files. This chapter will take you to know. How do you define the name of the uploaded file?

First, we use the STRSTR () function to intercept the name of the uploaded file and get the suffix of the uploaded file.

Then use the time () function to get the current timestamp of the system and redefine the name of the uploaded file.

File uploads are implemented using the Move_uploaded_file () function.

Before performing file uploads, in order to prevent potential attacks, illegal management of files that could not otherwise be scripted, the Is_uploaded_file () function should be used to determine if the specified file was uploaded via HTTP post and, if so, to return a true.

Is_uploaded_file () function

The Is_uploaded_file () function determines whether the specified file was uploaded via HTTP post with the following syntax:

Is_uploaded_file (filename)

The parameter filename must specify a variable similar to $_files[' filename ' [' tmp_name '] and cannot be used to upload a file name from the client $_files[' filename ' [' name ']

By using the Is_uploaded_file () function to determine the upload file, you can ensure that a malicious user cannot spoof the script to access files that are inaccessible, such as/etc/passwd.

Move_uploaded_file () function

The Move_uploaded_file () function uploads the file to the location specified in the server. True if success is returned. Otherwise, return false with the following syntax

Move_uploaded_file (filename,destination)

The parameter filename specifies the temporary filename of the uploaded file, i.e. $_files[tmp_name]; parameter destination refers to the new path and name saved after the file is uploaded. If the parameter filename is not a valid upload file, no action occurs.

Move_uploaded_file () will return false, and if the parameter filename is a valid upload file but cannot be moved for some reason, no action will occur, and Move_uploaded_file () returns false. A warning message is also issued.

Finally, using the RAND function, use random integers that it randomly generates as a new name for the uploaded file.

Application examples

This example redefine the name of the upload file, select the name of the file to be uploaded, click the Upload button, and the file will be uploaded to the Upfile folder in the local path specified in the program and redefine the file name.

The key code for this example is as follows:

<?                                       Phpheader ("Content-type:text/html;charset=utf-8"), if (!empty ($_files[up_picture][name])) { Determine if the upload file is empty if ($_files[' up_picture ' [' Error ']>0) {//Determine if the file can be uploaded to server E        Cho "Upload error";                Switch ($_files[' up_picture ' [' Error ']) {case 1:echo "the upload file size exceeds the profile specified value";            Break                Case 2:echo "Upload file size exceeds the agreed value in the form";            Break                Case 3:echo "Upload file not complete";            Break                Case 4:echo "File not uploaded";        Break }}else{if (!is_dir ("./upfile/")) {//determines if the specified directory exists mkdir ("./up                                             file/"); Create directory} $path = './upfile/'. Time (). Strstr ($_files[' up_picture ' [' Name '], '. ');           /define file name and storage location if (is_uploaded_file ($_files[' up_picture ' [' tmp_name '])) {//Determine if HTTP POST is uploaded if (!move_uploaded_file ($_files[' up_picture ' [' tmp_name '], $path)) {//Perform upload echo "Upload failed"; }else{echo "File". Time (). $_files[' up_picture ' [' Name ']. "            Upload succeeded, size: ". $_files[' up_picture ' [' Size ']; }}else{echo "Upload file". $_files[' up_picture ' [' Name ']. " Not legal!        "; }}}?>

defines the name of the uploaded file in the upload file program must be a feature, through which he can avoid the duplicate names of the file is replaced, alone may lead to the existence of a lot of the same content files. Read more about PHP file handling topics

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.