WordPress Implementation upload image automatically random rename

Source: Internet
Author: User
Tags php file save file sprintf time and seconds

In addition to the special features we may need, WordPress ordinary users to achieve a certain effect and function, basically can find the appropriate Plug-ins or document solutions. Because most of the company's customers Web sites are built by WordPress, some sites are delivered to customers manually by their own maintenance of updated content, there is a problem to be solved, such as products and content after the design of the image upload sometimes they use Chinese name or other name.

Because of the possible existence of special symbols, Chinese names, can cause the interface can not open, so the old Chiang thought of direct upload pictures to them using a random digital naming method. It could have been implemented with the file renaming on upload plug-in, but it reduced the plug-ins, so I used the plugin-free approach directly.

function Rename_upload_img ($file) {
$time =date ("y-m-d h:i:s");
$file [' name '] = $time. "". Mt_rand (100,999). ".". PathInfo ($file [' name '], pathinfo_extension);
return $file;
}
Add_filter (' Wp_handle_upload_prefilter ', ' rename_upload_img ');

Upload the above script to the functions.php file in the current topic, and then we will automatically rename the image to date + random numbers when we edit the article.

Code implementation methods:

To the root directory \wp-admin\includes\ download file.php to the local, with notepad++ open this file, with ctrl+f search $filename = wp_unique_filename ($uploads [' Path '], $ file[' name '], $unique _filename_callback); Add the code to the $filename before assigning the value to the following sentence:


Change Picture name

$name = basename ($filename, $ext); $time =SUBSTR (MD5 ($name), 0); $type _change = Array (' Image ' => '. '); $file _type = STRTR ($file [' type '], $type _change);
$file [' name '] = $time. '. Mt_rand (1,100). $file _type; $filename = Wp_unique_filename ($uploads [' Path '], $file [' name '], $unique _filename_callback);

Move the file to the uploads dir

Method Three


1, use the FTP tool to connect your PHP virtual host, find the WordPress program root directory of the/wp-admin/includes/file.php file, edit, find the following code, as shown below (you can also use the search function, search move the file To the uploads dir)

//Move the file to the uploads dir
$new _file = $uploads [' Path ']. "/$filename";
if (false = = @ Move_uploaded_file ($file [' Tmp_name '], $new _file)) {
if (0 = = Strpos ($uploads [' Basedir '], A Bspath))
$error _path = Str_replace (Abspath, ", $uploads [' Basedir ']). $uploads [' SubDir '];
Else
$error _path = basename ($uploads [' Basedir ']). $uploads [' subdir '];
return $upload _error_handler ($file , sprintf (' The uploaded file could not being moved to%s. '), $error _path));
}
2, replace the above code with the following: (Of course, you can also install the above code comments, easy to restore later)

Move the file to the uploads dir
$new _file = $uploads [' Path ']. "/$filename";
$new _file = $uploads [' Path ']. "/". Date ("Ymdhis"). Floor (Microtime () *1000). $ext;
if (false = = @ Move_uploaded_file ($file [' Tmp_name '], $new _file)) {
if (0 = = Strpos ($uploads [' Basedir '], Abspath))
$error _path = Str_replace (Abspath, ", $uploads [' Basedir ']). $uploads [' SubDir '];
Else
$error _path = basename ($uploads [' Basedir ']). $uploads [' SubDir '];
Return $upload _error_handler ($file, sprintf (' The uploaded file could is moved to%s. '), $error _path));
}
The main thing is to replace the first sentence with the second statement.

$new _file = $uploads [' Path ']. "/$filename";
$new _file = $uploads [' Path ']. "/". Date ("Ymdhis"). Floor (Microtime () *1000). $ext;
3, save file.php, so you can achieve WordPress upload images automatically renamed.

The above code means that, after saving to overwrite the original file, then the upload file will be "month-day time and seconds + thousands of milliseconds integer" format rename the file. This will no longer have to worry about the same picture file name and overwrite the original file.

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.