0

I build web application to manage spare parts for client,it comes with manual book as standard their part-id.
I checked its part-id are unique numbers. Storing records using MYSQL v5.5.16 and web interface using PHP v5.3.8.

As client requirement, they want to have spare parts image to be listed in folder. Basically, when user inputting record and upload a image, i renamed the image based on part-id stored into database and copied an uploaded image to folder.

Yes, it work well when user displaying images (part-id: 241203, 299301 ... etc). When part-id come with 1/8S40003, as you know image name couldn't consist with some characters (/ \ : * ? " < > |).

I also put <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> between <head> tags and header('Content-type: text/html; charset=utf-8'); on the top of page.

I simplified, just encode '/' sign to html decimal code -> &#047;.
Storing image name into database seem work well, uploading image work 100%, i also checking image manually inside folder,

but when displaying image with

<img src='../Images/Spare parts/1/8S40003.png' />

Web browser automatically decoded it into path directory ('/'), but i checked a tag on 'view source', it written with <img src='../Images/Spare parts/1&#047;8S40003.png' />.

Even using htmlentities, html_entity_decode, htmlspecialchar, htmlspecialchars_decode doesn't work and make it worse to convert '&' sign into &amp;

I found another way, by using urlencode from php, by convert '/' -> %2F and append the rest of number.

<img src='../Images/Spare parts/1%2F8S40003.png' />

I checked a tag on 'view source', it written similar with above but it doesn't give an solution.

Anyone, who ever had experienced with this problem before or ideas would be appreciated ! Thank you.

4

1 回答 1

0

正如 chirag 在他的回复中指出的那样,问题在于文件系统也不接受某些字符/。因此,您可能将图像存储&#047;在文件名中。

因此,要在 url 中使用那个混乱的图像名称,您需要使用:urlencode(str_replace('/','&#047;',$id))

如果您想出一个更可靠的从部件 ID 生成文件名的方案,例如通过将 rawurlencode 应用于文件名,那可能会更好。这将导致在 html 链接中引用图像时应用 url 编码两次,但它是可靠的:urlencode(rawurlencode($id))

于 2013-08-17T09:42:27.923 回答