For example, use the image height and width + file name to name
The code is as follows:
The code is as follows: |
Copy code |
Add_filter ('WP _ handle_upload_prefilter ', 'modify _ uploaded_file_names', 20 ); Function modify_uploaded_file_names ($ image ){ // Get default name of uploaded file and set to variable $ Imagename = $ image ['name']; // Case switch for multiple file extensions Switch ($ image ['type']) { Case 'image/jpeg ': $ Suffix = 'jpg '; Break; Case 'image/png ': $ Suffix = 'PNG '; Break; Case 'image/GIF ': $ Suffix = 'GIF '; Break; } // Get size of uploaded image and assign to variable $ Imagesize = getimagesize ($ image ); // Re-structure uploaded image name $ Image ['name'] = "{$ imagesize [0]} x {$ imagesize [1]}-{$ imagename}. {$ suffix }"; Return $ image; } |
For example, the system uses year, month, day, hour, minute, second, and kilobytes of milliseconds.
Take wordpress 3.2.1 as an example. Open the "wp-admin/website des/file. php" www.111cn.net file and find the 327th line of code:
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
The code is as follows: |
Copy code |
// Move the file to the uploads dir $ New_file = $ uploads ['path']. "/". date ("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.
The Yunqi community reminds you that these two methods are more suitable for the latter, because duplicate names are not displayed by year, month, day, hour, minute, second, and thousands of milliseconds, duplicate names may exist in the names of images based on height, width, and files.