Trash in file names or hiding our swans (wordpress)
- Tutorial
Introduction
Some cameras categorically refuse to name files based on timestamp using a regular counter. In everyday life, this trifle really seems to be a trifle, but on the Internet it grows into a problem that allows especially curious people to look for a site in search of photos P1100812.jpg and P1100813.jpg after viewing P1100811.jpg.
Options solutions:
add static prefix- add dynamic prefix
Now you need to decide how to add the prefix, whether to do it through a script to upload photos, renaming all local photos (the power of science, oh no!) Or at the time of uploading to the server. Without looking, without startled muscles of the face, I chose the option “to the server!”.
Technical implementation:
- she is! = write with pens
- she is not there! = ready-made plugin for wordpress
Fortunately or unfortunately, the plugin search returned an outdated list of old versions of wordpress. Therefore, armed with Google and then np ++, it was found that:
- processing file upload on shoulders
/wp-includes/functions.php
- file name * generated in function
wp_unique_filename
* generated - it’s I bent it a bit, it’s just checked for uniqueness / correctness and changed in the case, but this, judging by the code, is the final authority.
And so, open the file
functions.php
, find the function body in it wp_unique_filename
, there is a line $filename = sanitize_file_name($filename);
and joyfully edit it! How to edit it?
For example, like (one of the options):
$filename = crc32(md5(microtime()).sanitize_file_name($filename))."_".sanitize_file_name($filename);
$filename = md5(microtime().sanitize_file_name($filename))."_".sanitize_file_name($filename);
$filename = crc32("blablabla".microtime().sanitize_file_name($filename))."_".sanitize_file_name($filename);
$filename =
any soulful php hashes and anything random.sanitize_file_name($filename);
microtime()
- Performs the role of the PRNG, giving out the current server time. We leave the current operation algorithm
wp_unique_filename
, only adding our garbage to the beginning of the file. I chose the option of
CRC32(
magic of elves for myself )."_".sanitize_file_name($filename);
, since the CRC32
file (name) does not mutilate externally, adding a little number / letters, turning " random_file_names.png
" into digestible " 255298064_random_file_names.png
", which is more pleasant to the eye than " ae812e87bbbf3a068807fe763721c38a_random_file_names.png
". Total:
- simple one line solution
- private files are a bit more private (albeit in a public system)
Bonus:
3894406080_ P1100810.jpg and 3765414138_ P1100811.jpg In the same folder there is P1100827.jpg ... upd : Of course, you need to understand that when updating WP can update (= grind) functions.php. Therefore, this procedure will need to be repeated. Automation : Principle: find the line in the entire file and replace it with the one you selected (in the example, this ). Create the following file in the folder
$filename = sanitize_file_name($filename);
$filename = crc32(md5(microtime()).sanitize_file_name($filename))."_".sanitize_file_name($filename);
functions.php.diff
/wp-includes
For the prefix of the form "12345_image.jpg"
--- functions.php.orig 2015-04-20 06: 39: 25.000000000 +0000 +++ functions.php.new 2015-05-04 17: 45: 27.000000000 +0000 @@ -1 +1 @@ - $ filename = sanitize_file_name ($ filename); + $ filename = crc32 (md5 (mt_rand (). microtime ()). sanitize_file_name ($ filename)). "_". sanitize_file_name ($ filename);
For a suffix of the form "image_12345_.jpg"
--- old.php 2015-05-06 10:39: 17.847753325 +0000 +++ new.php 2015-05-06 10:39: 31.459794157 +0000 @@ -1 +1,2 @@ - $ filename = sanitize_file_name ($ filename); + $ filename = sanitize_file_name ($ filename); + $ filename = pathinfo ($ filename, PATHINFO_FILENAME) .'_ '. crc32 (md5 (mt_rand (). microtime ()). sanitize_file_name ($ filename)). (pathinfo ($ filename, PATHINFO_EXTENSION)?'. '. pathinfo ($ filename, PATHINFO_EXTENSION): '');
Run in the “dry” folder.
patch --dry-run functions.php -i functions.php.diff
If everything is OK, we get approximately the following answer:
checking file functions.php Hunk # 1 succeeded at 1862 (offset 1861 lines).
Then patch for real:
patch functions.php -i functions.php.diff
We get an answer like:
patching file functions.php Hunk # 1 succeeded at 1862 (offset 1861 lines).
The old file will have the name "
functions.php.orig
". As already mentioned above,
crc32
you can use a hash function instead , the values of which can be cut off due to bulkiness. Due to personal preferences, numbers from were left crc32
. upd2: (20150506) Added option for suffix between file name and extension. For files without extension (if downloading is allowed), the suffix will be added to the end.
upd3: (20150506) Added
mt_rand()
a shorter version for the suffix proposed by maximw .