简体   繁体   English

PHP getimagesize脚本问题

[英]Issue with PHP getimagesize script

I'm working on this simple script which finds an image url based upon database values then echos that to display the image, or if no image is available a default image. 我正在研究这个简单的脚本,该脚本根据数据库值查找图像URL,然后回显该图像以显示该图像,或者如果没有可用的图像为默认图像。

However there is something wrong as it returns the default image every-time. 但是,有些错误,因为它每次都会返回默认图像。 If I replace the default echo line with the $image line, it displays. 如果我将默认回显行替换为$ image行,它将显示。 So from that I know it can find the URL of the image. 因此,据我所知,它可以找到图像的URL。

<?php
$image = CONST_IMG_URL.$FetchData['capcode'].".jpg";            
error_reporting(0);

if (getimagesize($image)) {
echo "<a href='$image'><img src=\"$image\" width='300' height='150'/></a>";
} 

else {
echo "<img src='".plugins_url()."/plugins/images/image_not_found.jpg'     width='300' height='150'/>";}
?>

In your example, getimagesize isn't doing anything productive.. I would suggest using file_exists .. 在您的示例中, getimagesize不能产生任何效果。.我建议使用file_exists ..

In order to have file_exists work correctly - you need to give the full path (not a URI). 为了使file_exists正常工作-您需要提供完整路径(而不是URI)。

So you could do something like: $_SERVER['DOCUMENT_ROOT'] . '/path/to/images/' . $image; 因此,您可以执行以下操作: $_SERVER['DOCUMENT_ROOT'] . '/path/to/images/' . $image; $_SERVER['DOCUMENT_ROOT'] . '/path/to/images/' . $image;

like so: 像这样:

if(file_exists( '/full/path/to/image/dir/' . $image ))
{
    echo "<a href='$image'><img src=\"$image\" width='300' height='150'/></a>";
} else {
    echo "<img src='".plugins_url()."/plugins/images/image_not_found.jpg' width='300' height='150'/>";
}

Explanation 说明

getimagesize - This returns an array of attributes relating to the image, rather than explicitly returning if the image exists or not, in fact, if the image does not exist it will throw a warning rather than returning false as you'd expect. getimagesize返回与图像有关的属性array ,而不是显式地返回是否存在图像,实际上,如果图像不存在,它将抛出警告,而不是像您期望的那样返回false

file_exists - this checks for the file, and returns true if it exists, and false if the file does not exist - this is exactly what you're looking for in your scenario file_exists -这种检查的文件,返回true ,如果它存在, false如果文件不存在-这是你在寻找什么在您的方案

You should use next code: 您应该使用下一个代码:

try {
    $img = @getimagesize($image);
    echo "<a href='$image'><img src=\"$image\" width='300' height='150'/></a>";
} catch(Exception $e) {
    echo "<img src='".plugins_url()."/plugins/images/image_not_found.jpg'     width='300' height='150'/>";
}

As a guess, your CONST_IMG_URL is a path that's relative to the website - not the file-system. 推测一下,您的CONST_IMG_URL是相对于网站的路径,而不是文件系统。

The getimagesize() method finds images on the file-system itself using relative or absolute path names (ie /var/www/images/uploaded/pic.jpg or images/uploaded/pic.jpg ). getimagesize()方法使用相对或绝对路径名(即/var/www/images/uploaded/pic.jpgimages/uploaded/pic.jpg )在文件系统上查找图像。 It can also find images via URLs such as http://example.com/images/uploaded/pic.jpg . 它还可以通过诸如http://example.com/images/uploaded/pic.jpg URL查找图像。

If CONST_IMG_URL is a path, without the domain portion, getimagesize() will look at the file-system for that image and there's a good chance that it doesn't exist. 如果CONST_IMG_URL是一个没有域部分的路径,则getimagesize()将查看该图像的文件系统,并且很有可能不存在该图像。 For instance, if CONST_IMG_URL is /images/uploaded/ , the method will check on the server for that absolute path - which won't exist as it treats the leading / as "the root of the server", not "inside your website's folder". 例如,如果CONST_IMG_URL/images/uploaded/ ,则该方法将在服务器上检查该绝对路径-该路径将不存在,因为它将前导/视为“服务器的根目录”,而不是“位于网站文件夹内” ”。

To remedy this, you can take two approaches (if you want to keep using getimagesize() ). 为了解决这个问题,可以采用两种方法(如果要继续使用getimagesize() )。

  1. The first is the make CONST_IMG_URL an absolute path that includes your domain: 首先是使CONST_IMG_URL成为包含您的域的绝对路径:

     define('CONST_IMG_URL', 'http://domain.com/images/uploaded'); 
  2. The second would be to append the full document-root to the path when you're calling getimagesize(): 第二种是在调用getimagesize()时将完整的文档根附加到路径中:

     if (getimagesize($_SERVER['DOCUMENT_ROOT'] . '/' . $image)) { 

If you're going to go with #2 above, I would actually recommend using a different method, file_exists() instead. 如果您要使用上面的#2,我实际上建议您使用其他方法file_exists()代替。 This function will simply check if the file exists opposed to loading the file to pull parameter information out of it (width, height, etc) and will make page-loads slightly more efficient =]. 此函数将简单地检查文件是否存在,而不是加载文件以从中拉出参数信息(宽度,高度等),并使页面加载效率更高=]。

You should use file_exists function instead of getimagesize: 您应该使用file_exists函数而不是getimagesize:

<?php
$image = CONST_IMG_URL.$FetchData['capcode'].".jpg";            
error_reporting(0);

if (file_exists($image)) {
  echo "<a href='$image'><img src=\"$image\" width='300' height='150'/></a>";
} 
else {
  echo "<img src='".plugins_url()."/plugins/images/image_not_found.jpg'     width='300' height='150'/>";}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM