PHP file_exists() 异常

发布于 2024-11-19 22:42:53 字数 455 浏览 1 评论 0原文

我遇到了这个奇怪的问题:$myImg 变量已从一些本地 html 中提取并指向我想检查的文件。对于字符串变量 file_exists 给出 false,但如果手动插入 content os 变量,则给出 true。

var_dump($myImg);

输出:string(26) "content/images/1107_16.jpg"

var_dump(file_exists($myImg));

输出:bool(false)

var_dump(file_exists("content/images/1107_16.jpg"));

输出: bool(true)

怎么会发生呢? 感谢您提前提供的任何帮助

i ran into this wierd problem: the $myImg variable has been extracted from some local html and points to a file i would like to check. With the string variable file_exists gives false, but if the content os variable is inserted manually it gives true.

var_dump($myImg);

outputs: string(26) "content/images/1107_16.jpg"

var_dump(file_exists($myImg));

outputs: bool(false)

var_dump(file_exists("content/images/1107_16.jpg"));

outputs: bool(true)

How could it happen?
Thanks for any help in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

相守太难 2024-11-26 22:42:53

content/images/1107_16.jpg的长度不是45,所以显然还有其他字符。尝试修剪变量。

The length of content/images/1107_16.jpg is not 45, so obviously there are other characters. Try trimming the variable.

暗喜 2024-11-26 22:42:53
riad@riad-desktop:~$ php -r 'print(strlen("content/images/1107_16.jpg"));'
26

所以,你的字符串是坏的(可能在末尾包含一个坏字符(\0?)

riad@riad-desktop:~$ php -r 'print(strlen("content/images/1107_16.jpg"));'
26

So, Your string is bad (maybe consist of a bad characters (\0 ?) on the end

迟月 2024-11-26 22:42:53

我建议你使用bin2hex()来比较结果:

var_dump(bin2hex($myImg));
var_dump(bin2hex("content/images/1107_16.jpg"));

看来是不同字符集的问题。

I offer you to compare the results using bin2hex():

var_dump(bin2hex($myImg));
var_dump(bin2hex("content/images/1107_16.jpg"));

It seems that it's the problem of different charsets.

北笙凉宸 2024-11-26 22:42:53

在将字符串传递给 file_exists 方法之前尝试转换该字符串

$myImg = mb_convert_encoding($myImg, "UTF-8");

此外,您始终可以修剪附加到脏字符串的其他不需要的字符。

Try converting the string before passing it to file_exists method

$myImg = mb_convert_encoding($myImg, "UTF-8");

Additionally, you can always trim the other unwanted characters attached to the dirty string.

你对谁都笑 2024-11-26 22:42:53

也许你的 $myImg 中有一些看不见的字符?像换行符之类的东西?

perhaps some invisible character in your $myImg? something like a line break?

待"谢繁草 2024-11-26 22:42:53

您的字符串中似乎有一些无法打印的字符,或者不同的字符编码:

var_dump ("content/images/1107_16.jpg");
string(26) "content/images/1107_16.jpg"

请注意,这会正确报告 26 个字节 - 这对于 ASCII(或 UTF-8)来说是正确的。上面报告的 45 个字节看起来像是某种多字节编码。

You seem to be have some unprintable characters in your string, or a different character encoding:

var_dump ("content/images/1107_16.jpg");
string(26) "content/images/1107_16.jpg"

Note that this correctly reports 26 bytes - which would be right with ASCII (or UTF-8). The 45 bytes reported above would sem like some sort of multibyte encoding.

倾城花音 2024-11-26 22:42:53
string(45) "content/images/1107_16.jpg" 

应该少字符长尝试:

trim($myImg)

删除空格字符

string(45) "content/images/1107_16.jpg" 

should be less chars long try :

trim($myImg)

to remove spacechars

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文