PHP file_exists() 异常
我遇到了这个奇怪的问题:$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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
content/images/1107_16.jpg
的长度不是45
,所以显然还有其他字符。尝试修剪变量。The length of
content/images/1107_16.jpg
is not45
, so obviously there are other characters. Try trimming the variable.所以,你的字符串是坏的(可能在末尾包含一个坏字符(\0?)
So, Your string is bad (maybe consist of a bad characters (\0 ?) on the end
我建议你使用bin2hex()来比较结果:
看来是不同字符集的问题。
I offer you to compare the results using
bin2hex()
:It seems that it's the problem of different charsets.
在将字符串传递给 file_exists 方法之前尝试转换该字符串
此外,您始终可以修剪附加到脏字符串的其他不需要的字符。
Try converting the string before passing it to file_exists method
Additionally, you can always trim the other unwanted characters attached to the dirty string.
也许你的 $myImg 中有一些看不见的字符?像换行符之类的东西?
perhaps some invisible character in your $myImg? something like a line break?
您的字符串中似乎有一些无法打印的字符,或者不同的字符编码:
请注意,这会正确报告 26 个字节 - 这对于 ASCII(或 UTF-8)来说是正确的。上面报告的 45 个字节看起来像是某种多字节编码。
You seem to be have some unprintable characters in your string, or a different character encoding:
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.
应该少字符长尝试:
删除空格字符
should be less chars long try :
to remove spacechars