【问题标题】:php finding file with relative but not with absolute pathphp查找具有相对路径但不具有绝对路径的文件
【发布时间】:2016-08-03 06:42:27
【问题描述】:

我写了以下代码并得到了以下结果,但我不明白为什么。我也不知道或在网上找到任何解决方案。理想情况下,我希望它以所有四种方式工作。

<?php
// Note: the file containing this script is located in
// "http://www.example.com/code.php" and the directories as
// listed below are all correct in relation to this script.

$link1 = "https://www.example.com/folder/image.png";
$link2 = "http://www.example.com/folder/image.png";
$link3 = "/folder/image.png";
$link4 = "folder/image.png";

var_dump(getimagesize($link1),file_exists($link1));
// returns bool(false) bool(false)

var_dump(getimagesize($link2),file_exists($link2));
// returns bool(false) bool(false)

var_dump(getimagesize($link3),file_exists($link3));
// returns bool(false) bool(false)

var_dump(getimagesize($link4),file_exists($link4));
// returns array(6) { [0]=> int(192) [1]=> int(250)
// [2]=> int(3) [3]=> string(24) "width="192" height="250""
// ["bits"]=> int(8) ["mime"]=> string(9) "image/png" }
// bool(true)

echo "<img src=\"$link1\" />";
echo "<img src=\"$link2\" />";
echo "<img src=\"$link3\" />";
echo "<img src=\"$link4\" />";
?>

在所有四种情况下,使用&lt;img&gt; 标签都能正确显示图像。

【问题讨论】:

  • php.net/manual/en/function.getimagesize.php 是一个不错的开始。
  • @Marcus 我检查了,但在页面上是一个带有完整 URL 的示例(示例 3)。它没有提供更多细节,所以我想知道为什么我的示例不起作用。
  • 您是否输入了您的自己的 URL 进行测试?示例中的 URL (example.com) 实际上不会解析。您需要用您的服务器地址替换那些,例如。 http://www.**your-server**.com/**your_folder**/**your_image.png**
  • @Marcus 是的,我确实使用自己的服务器。出于简单和匿名的原因,我对其进行了更改
  • 好吧,您可能遇到了一些safe_mode 问题。根据file_exists() 文档:“警告此函数对由于安全模式限制而无法访问的文件返回 FALSE。但是,如果这些文件位于 safe_mode_include_dir 中,仍然可以包含这些文件。”这是唯一的其他原因file_exists() 会返回我能想到的false

标签: php image file-exists getimagesize


【解决方案1】:

更新

$link3 不起作用,因为file_exists() 一直在寻找/folder/ 一直到您的根目录。 file_exists() 不会像浏览器那样对待(相对)路径。

所以file_exists('/folder/image.png') 并没有从您的public 目录中提取出来,它一直以您输入/home/username/public_html//var/www/website/ 所期望的相同方式生根,知道我的意思?

输入file_exists('/path/to/your/public/dir/folder/image.png'); 即可。

如果尝试将http:// 链接添加到相关资产,file_exists() 将始终返回 false。它只解析服务器目录路径结构中的绝对路径。

【讨论】:

  • 我只是在编辑我的问题。目录是对的,四个链接都试了,图片都在里面。
猜你喜欢
  • 1970-01-01
  • 2015-01-18
  • 2011-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-17
  • 1970-01-01
  • 2010-12-16
相关资源
最近更新 更多