Fetching Images via PHP question.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ProjectGoldfish
    New Member
    • Mar 2007
    • 1

    #1

    Fetching Images via PHP question.

    Currently im taking this old piece of code
    [PHP]
    $fileName = $_GET["file"];
    $full = $_GET["full"];
    $ext = substr(strrchr( $fileName, "."), 1);
    $path = "../../../include/images/mini/";
    if($full)
    $path = "../../../include/images/full/";
    else
    $path = "../../../include/images/mini/";
    if(substr_compa re($_SERVER["HTTP_REFER ER"], $_SERVER["SERVER_NAM E"], 0, strlen($_SERVER["SERVER_NAM E"]))){
    //echo "<br>".$path.$f ileName."<br>";
    $size = getimagesize($p ath.$fileName);
    $fp = fopen($path.$fi leName, "rb");
    if ($size && $fp) {
    header("Content-type: {$size['mime']}");
    fpassthru($fp);
    exit;
    } else {
    echo "Unable to find file to open";
    // error
    }
    }else{
    return null;
    }[/PHP]

    And modify it into a funciton that will later be oved into a class. The code above runs perfectly when run by itself or via <img src="Linktothat code?file">

    However when modified into
    [PHP] function fetchImage($fil eName, $full){
    $ext = substr(strrchr( $fileName, "."), 1);
    $path = "../../../include/images/mini/";
    if($full)
    $path = "../../../include/images/full/";
    else
    $path = "../../../include/images/mini/";
    if(substr_compa re($_SERVER["HTTP_REFER ER"], $_SERVER["SERVER_NAM E"], 0, strlen($_SERVER["SERVER_NAM E"]))){
    //echo "<br>".$path.$f ileName."<br>";
    $size = getimagesize($p ath.$fileName);
    $fp = fopen($path.$fi leName, "rb");
    if ($size && $fp) {
    header("Content-type: {$size['mime']}");
    fpassthru($fp);
    exit;
    } else {
    echo "Unable to find file to open";
    // error
    }
    }else{
    return null;
    }
    }[/PHP]

    All that prints to the test document is the called url.
    The html of the document shows the <img src="the_ascii_ representation_ of_the_images_b inary">

    Here is the code for the test document.
    [PHP]
    <?php
    include ("../../../include/fetchImage.php" );
    ?>
    <img src="<?php fetchImage("e49 3cac401eab12d8a 9ebf989a0efd3ec 47e31601.jpg", 1) ?>" id=\"imgTop\" onmouseover=\"r ollover(0)\">[/PHP]

    I have assured that all files exist in the directories that im specifying.

    What is so different about the 2 different fetching methods that would cause such a differerence in result?
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    The original code put the image hex-binary-whatever straight into the output, without any <img /> tag, did it not?

    If so the problem could be that the browser is expecting only that hex-binary-whatever thing but instead it is getiing html tags, at wich point it stops rendering the hex-binary-whatever and starts rendering text/html.

    If you were to create a seperate .php file that would work like the old code, lets call it img.php, and call that in your <img /> tag.

    For example.
    [HTML]<img src="img.php?pa th=myimg.jpg" />[/HTML]

    Then it would probbly show the image like you want.

    Comment

    Working...