Yesterday to do a project, which has a demand for each picture corresponding to a small paragraph of text on the picture, the common practice is to create a new table and then the picture name and description text are recorded in the database. After careful consideration of this application do not have the database can also be completed, I implemented the program is the description of the text after urlencode as a filename, so that when I read the file name UrlDecode can be later flooding pictures of the text description.
However, access to the picture through the browser can not find the file, such as a picture of the description of the text as "June blog", UrlEncode generated after the file name is as follows
Copy Code code as follows:
%e7%90%bc%e5%8f%b0%e5%8d%9a%e5%ae%a2.jpg
So I access the picture through the browser, the hint can not find
After a careful look, I found that when the browser access to automatically put the filename back to Chinese
Firefox
Chrome
Ie
Safari
IE and Safari are not seen in the address bar to convert to Chinese characters, but they also hint that the file cannot be found. But I feel that it should be when it is requested to automatically turn, but the address bar does not show after the conversion. Viewing requests for access to pictures from Nginx access records
Copy Code code as follows:
192.168.6.30--[12/oct/2012:10:09:44 +0800] "Get/%e7%90%bc%e5%8f%b0%e5%8d%9a%e5%ae%a2.jpg http/1.1" 404 199 "-" "Mozil la/5.0 (compatible; MSIE 9.0; Windows NT 6.1; trident/5.0) "
Request URL processing did not find anything unusual, and finally through the repeated study of the encoded file name, found by the hundred semicolon and alphanumeric composition, I feel that the browser should be encountered with the percent sign may do some other conversion processing, so that the browser access to the UrlEncode after the file hint found.
So I took all the urlencode in the filename and replaced it with an underscore.
Copy Code code as follows:
%e7%90%bc%e5%8f%b0%e5%8d%9a%e5%ae%a2.jpg
Replaced by
Copy Code code as follows:
_e7_90_bc_e5_8f_b0_e5_8d_9a_e5_ae_a2.jpg
Re-use browser access, problem resolution
If you want to get the text description of the picture, direct the filename "_" replaced "%" and then use UrlDecode can be.
Finally, it should be noted that Linux under the name of the same as the win system has a length limit, the most commonly used format is ext3, which allows the format of 255 characters, minus about 5 as extension name after approximately the remaining 250 length of pure file name, The length of a Chinese character after UrlEncode is 9, so the maximum number of 27 characters can be encoded as a filename .
Although this way to store less Chinese characters, but you can use some encryption methods to obtain a short series of ciphertext, and then the ciphertext UrlEncode can be, the specific implementation of the way I do not one of the examples, do some thinking about it!