简体   繁体   中英

Path For PHP getimagesize() Function

For getimagesize(), what is the proper path to use for the file? In my case, I prefer an absolute path but it gives an error that there is no file but when I give it a full URL, no errors but no output either. The images show but not the attributes from getimagesize().

To clarify, the images are stored in a database with a script pulling them out and providing the headers which, in this case, is doing so from within a foreach loop as it is pulling out multiple images for a dynamic menu. I've done this successfully in the past on other sites and this code is from one of those working sites so not sure why it won't work here!

The question is, what is the proper path type and why no output?

This (preferred) gives errors of no image:

list($width, $height, $type, $attr) = getimagesize("/internals/viewers/show_image.php?ID=$MenuImage");
$imagetags = (!empty($attr)) ? $attr : "";

This does not but no attributes either:

list($width, $height, $type, $attr) = getimagesize("http://domain.loc/internals/viewers/show_image.php?ID=$MenuImage");
$imagetags = (!empty($attr)) ? $attr : "";

Images being displayed using:

echo "$MenuLink<img src=\"/internals/viewers/show_image.php?ID=$MenuImage\" alt=\"$MenuName\" title=\"$MenuDescription\" class=\"MenuTab\"$imagetags></a>\n";

Here is show_image.php:

<?php include $_SERVER ['DOCUMENT_ROOT'] . "/internals/configuration/common.php";

$ImageID = (isset($_GET['ID'])) ? $_GET['ID'] : "";

if (!$ImageID) die ("No image");

    $sqlImage = "SELECT Image, ImageFormat FROM images WHERE ID='$ImageID'";
    $rowImage = DBConnect($sqlImage, "Select", $siteDB);

if (!empty($rowImage)) :
    $mime_type = $rowImage['ImageFormat'];
    $SiteImage = $rowImage['Image'];
    header("Pragma: no-cache");
    header("Content-type: image/$mime_type");
    echo $SiteImage;
    imagedestroy($SiteImage);
endif; ?>

Assuming you're running this on Linux or MacOS, starting a path with / starts it at the root of the entire hard drive . Because PHP runs server-side, it doesn't know where the root of your web server directory is. It would be better to use something like /opt/apache/htdocs/internals/viewers (just an example), or a relative path. I generally use relative paths.

EDIT: You can try using getimagesize($_SERVER['DOCUMENT_ROOT'] . "internals/viewers/show_image.php?ID=$MenuImage") , which will likely work in your case.

ANOTHER EDIT: Because your image isn't actually an image file, you can pass an http:// or https:// URL into your getimagesize() call. Here's an example: getimagesize("http://" . $_SERVER['HTTP_HOST'] . "/internals/viewers/show_image.php?ID=$MenuImage");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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