在php中,斜杠显示为正斜杠 [英] Addslashes displays as forward slashes in php

查看:425
本文介绍了在php中,斜杠显示为正斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我想要访问的服务器上的文件。文件名是 ken\的book.doc

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

但在我的数据库中,它被存储为 ken的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.

我使用addslashes添加反斜杠,但它显示为: ken /'s的book.doc 这是一个正斜杠而不是反斜杠。

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.

我已经使用过:

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

显示为正斜杠。

我如何解决这个问题?

谢谢

编辑

额外信息:我正在使用新值作为链接的一部分,即: / p>

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

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


推荐答案

如果你有一个文件名t帽子在磁盘上包含一个反斜杠,我会先修复它。您的第二个问题似乎在存储该文件名时不使用 mysql_real_escape_string 进入数据库(为什么最终没有反斜杠)。

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本身不添加正斜杠。你的故事的那部分是不真实的。要再次删除它们,您将不需要古怪的 str_replace 呼叫,但只是 stripslashes

实际问题(编辑之后)原来是一个html链接。这只是因为浏览器有将反斜杠变成url中的正斜杠的习惯。为了防止这种情况适用, urlencode()

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中,斜杠显示为正斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆