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