【问题标题】:Using file_exists to check if image exist before echo在 echo 之前使用 file_exists 检查图像是否存在
【发布时间】:2013-01-29 20:05:27
【问题描述】:

我试图让我的脚本在将图像回显到我的表格之前检查它是否存在。如果它不存在,它应该替代另一个图像。我的问题是,即使图像存在,它仍然属于 else 因素。谁能发现任何编码错误?

<?php
mysql_select_db("xxxx");
$result = mysql_query("SELECT * FROM yyyy WHERE frivillig=1 ORDER BY stilling");
while($row = mysql_fetch_array($result))
{
?>
<tr>
<td width="50px" class="frivillig"><?php  if (file_exists($url . "ansatte" . "/" . (utf8_encode($row['etternavn'])) . "%20" . (utf8_encode($row['fornavn'])) . ".jpg")) {
echo "<img" . " " . "src='" . $url . "ansatte" . "/" . (utf8_encode($row['etternavn'])) . "%20" . (utf8_encode($row['fornavn'])) . ".jpg'" . " " . "height='50px'/>";
}
else {
echo "<img" . " " . "src='" . $url . "images/mangler.png'" . " " . "height='50px'/>";
}
?>

如您所见,我使用 $url 作为我的完整 url,并且图像放置在 /ansatte/ 文件夹中。

感谢您的帮助!

【问题讨论】:

  • 我发现了一个编码错误。停止使用mysql_* 函数。使用PDOmysqli_*

标签: php file-exists


【解决方案1】:

考虑下面的 sn-p 检查远程资源:

$path = $url . "ansatte" . "/" . (utf8_encode($row['etternavn'])) . "%20" . (utf8_encode($row['fornavn'])) . ".jpg"

$ch = curl_init($path);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE);
$response = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if ($httpcode < 400) { // Filter out bad request, not found, etc
 // found
} else {
 // not accessible
}

如果您没有安装 php5-curl,您还可以查看以下替代方案:

获取图像大小

这里只支持某些流,但使用很简单,它会更好地验证返回:

if (getimagesize($path) !== FALSE) {
  //exists
}

打开

if (fopen($path,"r") !== FALSE) {
  // exists
}

get_headers

$headers = get_headers($path, TRUE);
if ($headers !== false && isset($headers['Content-Type']) && $headers['Content-Type'] == 'image/jpeg') {
  // exists
}

【讨论】:

  • 也可以使用PECL_HTTP、Zend 或Pear。允许发送正确的 HTTP 请求并捕获响应代码的任何内容。
  • @crush 是的,fopengetimagesize 可能是值得的替代品,并且不需要插件。
  • 我认为fopen 会与file_exists 陷入同样的​​问题?至少,如果服务器主机为无效 url 提供错误重定向页面,它仍然会返回一个句柄。
  • @crush 是的,它会.. 如果他控制网络服务器上的配置,那应该不是问题。或者,get_header 可用于验证content-type。我会将它们添加到我的答案中。
  • 如果 URL 无效或无法解析,get_headers() 会触发警告。可能想要禁止对其调用发出警告,因为您事后要手动检查它。
【解决方案2】:

file_exists() 不适用于检查 URL。您对 file_exists() 的调用应反映相对或绝对文件系统路径,而不是 URL。

【讨论】:

  • 来自手册:“从 PHP 5.0.0 开始,这个函数也可以与一些 URL 包装器一起使用。”
  • @Mike 我刚刚发布了那个。这是一个列表:php.net/manual/en/wrappers.php
  • @Mike “也可以”并不意味着“应该”。 file_exists() 是否足够智能以检测 404 错误,还是仅在网络服务器完全没有响应时才返回 false?
  • @Mike 在使用 PHP 5.3.20 对其进行测试后,我可以告诉您,即使使用 http URL 的文件确实存在,该函数似乎也会返回 false。所以file_exists()可能是不适合用于海报的功能。
【解决方案3】:

我在这里预感到并假设图像文件实际上与脚本位于同一台服务器上。如果是这样,您应该在检查文件是否存在时使用绝对或相对路径,而不是 URL。

<?php

$stmt = $dbh->prepare("SELECT * FROM yyyy WHERE frivillig=1 ORDER BY stilling");
$stmt->execute();
$rows = $stmt->fetchAll();

$url = 'http://www.yoursite.com/subdir/';
$path = '/path/to/yoursite/subdir/';

foreach ($rows as $row) {
    ?>
    <tr>
    <td width="50px" class="frivillig">
    <?php
    if (file_exists($path . "ansatte" . "/" . (utf8_encode($row['etternavn'])) . "%20" . (utf8_encode($row['fornavn'])) . ".jpg")) {
        echo "<img" . " " . "src='" . $url . "ansatte" . "/" . (utf8_encode($row['etternavn'])) . "%20" . (utf8_encode($row['fornavn'])) . ".jpg'" . " " . "height='50px'/>";
    }
    else {
        echo "<img" . " " . "src='" . $url . "images/mangler.png'" . " " . "height='50px'/>";
    }
}
?>

正如@crush 提到的,你不应该使用mysql_* 函数。自 PHP 5.5 起,它们已被弃用。上面的例子使用了 PDO。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-11
    • 1970-01-01
    • 2014-01-16
    • 1970-01-01
    • 2018-07-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多