在 PHP 中使用 Web 域或系统路径 URL 访问文件
我的计算机上有 localhost Apache/PHP 配置。我希望能够
1)使用绝对路径检查图像文件是否存在,
2)使用相同的 URL 显示它
我想做的事情的一个简单示例:
if(file_exists($url)) {
echo '<img src="' . $url . '" />';
}
如果我设置...
$url = 'http://' . $_SERVER['HTTP_HOST'] . '/myimg.jpg';
那么 file_exists 将返回 false,因为 URL 是 Web 路径 (http://localhost/myimg.jpg)。
如果我设置...
$url = $_SERVER['DOCUMENT_ROOT'] . '/myimg.jpg';
它将识别该文件存在(C:/htdocs/myimg.jpg),但无法显示图像,因为它无法访问图像源。如果我查看源代码并将 URL 复制/粘贴到 Web 浏览器的地址栏中,图像就会正常显示。
也许我在 Apache 的 httpd.conf 文件或 PHP.ini 中缺少某些内容?也许是某种别名声明。此外,网站完成后将上传到远程 Web 服务器(例如 bluehost.com),因此我需要一个多功能的解决方案。
PHP.ini 中的allow_url_fopen 处于打开状态,每次在httpd.conf 或PHP.ini 中进行更改时,我都会重新启动服务器。我已经为此苦苦挣扎了好几天,所以即使是正确方向的一点也将非常感激:)
谢谢。
I have a localhost Apache/PHP configuration on my computer. I want to be able to
1) check the existence of an image file using an absolute path and
2) display it using the same URL
A simple example of something I want to do:
if(file_exists($url)) {
echo '<img src="' . $url . '" />';
}
if I set...
$url = 'http://' . $_SERVER['HTTP_HOST'] . '/myimg.jpg';
then file_exists will return false, because the URL is a web path (http://localhost/myimg.jpg).
if I set...
$url = $_SERVER['DOCUMENT_ROOT'] . '/myimg.jpg';
it will recognize that the file exists (C:/htdocs/myimg.jpg), but fail to display the image because it cannot access the image source. If I view the source and copy/paste the URL into the web browser's address bar, the image is displayed just fine.
Perhaps I'm missing something in Apache's httpd.conf file, or PHP.ini? Maybe an Alias declaration of some sort. Also, the site will be uploaded to a remote web server (such as bluehost.com) when it is complete, so I need a versatile solution.
allow_url_fopen in PHP.ini is On, and I've been restarting the server every time I make a change in either httpd.conf or PHP.ini. I've been struggling with this for days, so even a point in the right direction would be very appreciated :)
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您将服务器端文件访问与客户端混淆了。
这会生成一个 URI,客户端可以使用该 URI 通过 HTTP 获取您的图像。例如
http://example.com/myimg.jpg
这会生成 PHP 使用的文件系统路径。例如
/var/www/htdocs/myimg.jpg
您的解决方案需要同时使用
编辑:您可能可以将
echo
行替换为绝对 URL(以正斜杠)始终从文档根开始
You're confusing server side file access with client side.
This produces a URI to be used by the client to fetch your image via HTTP. For example
http://example.com/myimg.jpg
This produces a filesystem path to be used by PHP. For example
/var/www/htdocs/myimg.jpg
Your solution needs to use both
Edit: You could probably replace the
echo
line withas an absolute URL (one beginning with a forward slash) always starts at the document root
file_exists
采用的绝对路径与您的 Web 路径完全不同。您只需在img
标记中为file_exists
和src
属性使用单独的路径即可。An absolute path, which
file_exists
takes, is completely different from your web path. You'll just need to use separate paths forfile_exists
and yoursrc
attribute in yourimg
tag.这将为您提供 HTML:
这是图像,因为它是从域名解析的。
This will give you the HTML:
Which is the image since it resolves from the domain name.