极
极核实验室
Tag archive

#文件

Browse all WindBlog tags and find articles by topic.

Articles: 1 View all tags
Hand-write a plugin to add a timestamp to the end of uploaded filenames to prevent excessive SQL queries
技术文章 WordPress WordPress插件 • • 1

Hand-write a plugin to add a timestamp to the end of uploaded filenames to prevent excessive SQL queries

In the WordPress Media Library, when a file is uploaded, it first queries the database to check whether a file with the same filename exists; if it does, it continues to query the next filename. This causes the number of SQL requests to keep increasing after repeatedly uploading a file with the same filename (even if the file contents are different). Many plugins currently either prepend a timestamp to the filename or directly rename the filename to a timestamp, which makes finding files very difficult. For this reason, I recommend getting the extension, adding a timestamp to the end of the filename, and then appending the extension back. Below is the complete code; you can