How to redefine the instance of the uploaded file name

Source: Internet
Author: User
When uploading files, you must specify the path of the uploaded files. However, if a large number of users upload different files, there will inevitably be a possibility of duplicate names. this will cause file replacement, leading to file loss, therefore, it is very important to redefine the name of the uploaded file. This chapter will show you more. How to define the name of the uploaded file? When uploading files, you must specify the path of the uploaded files. However, if a large number of users upload different files, there will inevitably be a possibility of duplicate names. this will cause file replacement, leading to file loss, therefore, it is very important to redefine the name of the uploaded file. This chapter will show you more. How to define the name of the uploaded file?

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

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

Use the move_uploaded_file () function to upload files.

To prevent potential attacks and illegally manage files that cannot be interacted with by scripts, use is_uploaded_file () the function checks whether the specified file is uploaded through http post. If yes, a true value is returned.

Is_uploaded_file () function

The is_uploaded_file () function is used to determine whether the specified file is uploaded through http post. Its syntax is as follows:

is_uploaded_file(filename)

The filename parameter must specify a variable similar to $ _ FILES ['filename'] ['tmp _ name, the file name uploaded from the client is not allowed. $ _ FILES ['filename'] ['name']

The key code in this example is as follows:

 0) {// Determine whether the file can be uploaded to the server echo "upload error"; switch ($ _ FILES ['up _ picture '] ['error']) {case 1: echo "the size of the uploaded file exceeds the specified value of the configuration file"; break; case 2: echo "the size of the uploaded file exceeds the specified value in the form"; break; case 3: echo "Incomplete file uploads "; break; case 4: echo "no file uploaded"; break ;}} else {if (! Is_dir (". /upfile/") {// Determine whether the specified directory contains mkdir (". /upfile/"); // create a directory} $ path = '. /upfile /'. time (). strstr ($ _ FILES ['up _ picture '] ['name'],'. '); // define the file name and storage location if (is_uploaded_file ($ _ FILES ['up _ picture'] ['tmp _ name']) {// Determine whether the if (! Move_uploaded_file ($ _ FILES ['up _ picture '] ['tmp _ name'], $ path) {// execute the upload echo "Upload failed ";} else {echo "file ". time (). $ _ FILES ['up _ picture '] ['name']. "uploaded successfully. size :". $ _ FILES ['up _ picture '] ['size'] ;}} else {echo "Upload file ". $ _ FILES ['up _ picture '] ['name']. "Illegal! ";}}?>

Defining the name of a file to be uploaded must be an essential function in the file upload program. this feature can prevent the replacement of the file with the same name. a single file may lead to many files with the same content. For more information, see php file processing.

[Recommended related articles]

Php file upload-how to use the move_uploaded_file () function

The above is the detailed description of how to redefine the name of the uploaded file. For more information, see other related articles in the first PHP community!

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.