Upload a wordpress file to the server and change the file name

Source: Internet
Author: User
Tags sprintf

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.

 

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.