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 ); |