检查文件是否存在与 php 不工作,需要帮助
请告诉我,我的以下代码有什么问题:
<a href="<?php echo $_url; ?>" title="<?php echo $_name; ?>">
<?php
$logo2 = $_url.'/image/data/logo2.png';
$logo = $_url.'/image/data/logo.png';
if (file_exists($logo2)) {
echo "<img src=".$logo2." alt=\"Logo\" style=\"border: none;\" />";
} else {
echo "<img src=".$logo." alt=\"Logo\" style=\"border: none;\" />";
} ?>
</a>
$logo2 和 $logo 的图像都存在于同一目录中,但代码只显示 $logo (logo.png) 我需要指针并提前致谢
更新:
$_url 的值是
$this->data['_url'] = $this->config->get('config_url');
当我 时,将显示例如 http:// www.mysite.com
使用上面的代码仅显示 logo.png
Please advise me, what wrong with my following code:
<a href="<?php echo $_url; ?>" title="<?php echo $_name; ?>">
<?php
$logo2 = $_url.'/image/data/logo2.png';
$logo = $_url.'/image/data/logo.png';
if (file_exists($logo2)) {
echo "<img src=".$logo2." alt=\"Logo\" style=\"border: none;\" />";
} else {
echo "<img src=".$logo." alt=\"Logo\" style=\"border: none;\" />";
} ?>
</a>
both images of $logo2 and $logo exists in the same directory, but the code only shows $logo (logo.png)
I need pointers and thanks in advance
UPDATED:
the value of $_url is
$this->data['_url'] =
$this->config->get('config_url');
and when i <?php echo $_url;?>
that will show e.g. http://www.mysite.com
by using code at above only show logo.png
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
file_exists
可用于 URL 包装器。在您的情况下,如果您确实需要执行 URL 包装器检查(会非常慢),请确保启用 URL 包装器(默认启用)。
另外,您的
$_url = http://www.mysite.com ///image/data/logo2.png
,注意额外的斜杠可能会影响Web服务器重写。如果该文件与您的 Web 服务器位于同一服务器上,则应将
$_url
替换为 document_root(文件夹路径)。对于函数而言,
file_exists
对于目录也返回 true。您应该将其替换为is_file
file_exists
can be used for URL wrapper.In your case, if you really need to perform URL wrapper checking (will be very slow), make sure URL wrapper is enabled (default is enabled).
And also, your
$_url = http://www.mysite.com///image/data/logo2.png
, take note the extra slash may affecting web server rewrite.If the file is located at the same server as your web server, you should replace the
$_url
to document_root (path to the folder).For function wise,
file_exists
return true for directory too. You should replace that tois_file
您正在将
file_exists()
应用于无效的 URL。您需要将其应用到文件系统路径。
You are applying
file_exists()
to a URL which doesn't work.You need to apply it to a filesystem path.
file_exists
需要一个本地路径,而不是url。file_exists
expects a local path, not a url.与这里的一些答案相反,file_exists 可以将 URL 作为参数,它会检查它是否存在。但是,您最好还是使用 file_exists 的文件系统路径而不是 URL。
无论如何,我立即想到两个原因:
两个文件是否具有相同的权限? (即,logo.png 可能具有必要的读取权限,而 logo2.png 可能没有这些权限)
文件名真的与脚本中的相同吗?例如,在您的开发平台上,一切都可能正常工作 - Mac 或 Windows 会忽略文件名的字母大小写,但在 Linux 服务器上则不然,文件名必须采用相同的大小写。
Contrary to some answers here, file_exists can take an URL as a parameter and it will check whether it exists or doesn't. However, you're still better off using a filesystem path for file_exists instead of the URL.
Anyway, two reasons immediately come to mind:
Do both files have the same permissions? (I.e., logo.png might have the necessary read permissions and logo2.png might not have them)
Are the file names really the same as in the script? For example, everything might work fine on your development platform - a Mac or Windows which ignores letter case for filenames but not on a Linux server where the filename must be in the same case.
使用 getimagesize() 因为 file_exists 将返回 false。
Use getimagesize() as file_exists will return false.