Addslashes displays as forward slashes in php

2019-08-23 21:57发布

问题:

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>


标签: php escaping