Php file upload and Image rename methods summary

Source: Internet
Author: User
Tags php file upload
Renaming uploaded files or images is a problem we will encounter during php uploads. renaming is nothing more than generating a random string for the files you upload, including Md5, the name is random or renamed by time and date. I will introduce it to you later. For example, we are uploading...

Renaming uploaded files or images is a problem we will encounter during php uploads. renaming is nothing more than generating a random string for the files you upload, including Md5, the name is random or renamed by time and date. I will introduce it to you later.

Perform the following operations on the Upload file page:

The code is as follows:


$ Name = $ _ FILES ['userfile'] ['name']; // assign the name of the uploaded file to the name

Then we need to rename it.

The code is as follows:

// Automatic generation of file names

List ($ usec, $ new_name) = explode ("", microtime ());

If (@ $ extension_name) {// The file types are jpg, gif, png, and bmp.

Move_uploaded_file ($ tmp_name, $ new_dir. '/'. $ new_name. '.'. $ extension_name); // move to the target folder

Echo 'upload successful ';

In this way, a new file name is generated based on the time and date.

Example

The code is as follows:

The code I added to rename the file name to the system time is as follows:

The code is as follows:


$ Date = date (ymdhis );
// Echo ($ date );
If ($ _ FILES ["uppic"] ["type"] = "image/gif ")
| ($ _ FILES ["uppic"] ["type"] = "image/jpeg ")
| ($ _ FILES ["uppic"] ["type"] = "image/pjpeg "))
& ($ _ FILES ["uppic"] ["size"] <2000000 ))
{
If ($ _ FILES ["uppic"] ["error"]> 0)
{
Echo "Return Code:". $ _ FILES ["uppic"] ["error"]."
";
}
Else
{
// Echo "Upload:". $ _ FILES ["uppic"] ["name"]."
";
// Echo "Type:". $ _ FILES ["uppic"] ["type"]."
";
// Echo "Size:". ($ _ FILES ["uppic"] ["size"]/1024). "Kb
";
// Echo "Temp file:". $ _ FILES ["uppic"] ["tmp_name"]."
";
$ Uptype = explode (".", $ _ FILES ["uppic"] ["name"]);
$ Newname = $ date. ".". $ uptype [1];
// Echo ($ newname );
$ _ FILES ["uppic"] ["name"] = $ newname;

If (file_exists ("pics/". $ _ FILES ["uppic"] ["name"])
{
Echo $ _ FILES ["uppic"] ["name"]. "already exists .";
}
Else
{
$ _ FILES ["uppic"] ["name"] =
Move_uploaded_file ($ _ FILES ["uppic"] ["tmp_name"],
"Pics/". $ _ FILES ["uppic"] ["name"]);
Echo "script" alert ('upload successful! ') Script ";
Echo "script window. location. href = 'http: // www.phprm.com '; script";
}
}
}
Else
{
Echo "Invalid file ";
}
?>

Now, let's talk about it. you can refer to it.



Article URL:

Reprint ^ at will, but please attach the tutorial address.

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.