Chinese garbled characters in WordPress uploaded files (automatically renamed)

Source: Internet
Author: User
Tags md5 sprintf

The following uses wordpress 3.2.1 as an example to open the wp-admin/shortdes/file. Php file and find the code in line 326th:

The code is as follows: Copy code

// Move the file to the uploads dir
$ New_file = $ uploads ['path']. "/$ filename ";
If (false ===@ move_uploaded_file ($ file ['tmp _ name'], $ new_file ))
Return $ upload_error_handler ($ file, sprintf (_ ('The uploaded file cocould not be moved to % s. '), $ uploads ['path']);

Modify it

// Move the file to the uploads dir
$ New_file = $ uploads ['path']. "/". date_i18n ("YmdHis"). floor (microtime () * 1000). ".". $ ext;
If (false ===@ move_uploaded_file ($ file ['tmp _ name'], $ new_file ))
Return $ upload_error_handler ($ file, sprintf (_ ('The uploaded file cocould not be moved to % s. '), $ uploads ['path']);

Save and upload the file again. In this way, the newly uploaded file will be automatically saved as the new file name "year, month, day, hour, minute, and thousands of milliseconds integer" and saved to the corresponding year and month folder. That's right. It's that simple, testing, and passing. This method is recommended for English foreign trade websites of European and American customers.

Some wordpress daemon do not need to be operated on the file. Php file, but only in the functions. Php file. This method is more convenient.

Add the following code to functions. php:

The code is as follows: Copy code

Function new_filename ($ filename ){
$ Info = pathinfo ($ filename );
$ Ext = empty ($ info ['extension'])? '': '.'. $ Info ['extension'];
$ Name = basename ($ filename, $ ext );
Return md5 ($ name). $ ext;
}
 
Add_filter ('sanitize _ file_name ', 'New _ filename', 10 );

After the file is added and saved, the file name is automatically renamed. A 32-bit md5 encrypted file name is automatically generated. If you know that the 32-bit file name is too long
You can use substr () to extract the desired length. The code is as follows. I use 15 characters.

The code is as follows: Copy code

Function new_filename ($ filename ){
$ Info = pathinfo ($ filename );
$ Ext = empty ($ info ['extension'])? '': '.'. $ Info ['extension'];
$ Name = basename ($ filename, $ ext );
Return substr (md5 ($ name), 0, 15). $ ext;
}
 
Add_filter ('sanitize _ file_name ', 'New _ filename', 10 );

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.