Addslashes displays as forward slashes in php

橙三吉。 提交于 2019-12-25 03:45:43

问题


I have a file on my server that i want to access. The filename is ken\'s book.doc

But in my db, it was stored as ken's book.doc

(I have fixed the backslash issue, but still have problems accessing the previously uploaded files on server.

I used addslashes to add the back slash but it displays it as: ken/'s book.doc (that is a forward slash instead of a backslash.

I have used:

str_replace("'", "\'", $filename);

yet it displays as a forward slash.

How can i fix this?

Thanks

EDIT

Extra Information: I am using the new value as part of a link. that is:

<a href="<?php echo str_replace("'", "\'", $filename);?>">View</a>

回答1:


If you have a filename that contains a backslash on disk, I would fix that first. Your second problem was appearantly not using mysql_real_escape_string when storing that filename into the database (why it ended up there without backslash).

addslashes btw does not add forward slashes by itself. That part of your story is untrue. And to remove them again you wouldn't need the quirky str_replace call, but just stripslashes.

The actual problem (after your edit) turns out to be a html link. That's simply because browsers have the habit of turning backslashes into forward slashes in urls. To prevent that apply urlencode()

 <a href="<?=urlencode(stripslashes($filename));?>">View</a>


来源:https://stackoverflow.com/questions/6324144/addslashes-displays-as-forward-slashes-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!