Jump to content

How to get an image from outside public HTML?


lopes_andre

Recommended Posts

Hi,

 

I need to get images from outside public_html. I have tested this script, and it works:

 

<?php  

function getImageFromOutside($filePath, $folder, $file)  {  

    if (file_exists($filePath . $folder . $file))
    {
        $contents = file_get_contents($filePath . $folder . $file);
        return $contents;
    }

}

header('Content-type: image/jpeg');
echo getImageFromOutside('C:/xampp/images/', '1/', '1.jpg');

?>

 

But this script does not do what I want, I need to display images and use HTML Tags, like this:

 

<?php  

function getImageFromOutside($filePath, $folder, $file)  {  

    if (file_exists($filePath . $folder . $file))
    {
        $contents = file_get_contents($filePath . $folder . $file);
        return $contents;
    }

}

header('Content-type: image/jpeg');
echo "<body>";
echo getImageFromOutside('C:/xampp/images/', '1/', '1.jpg');
echo "</body>";
?>

 

If I use HTML Tags, the image does not show.

 

How can I solve this problem?

 

 

PS: Sorry for my english.

 

 

Best Regards.

You need to change your first script a little, and place it within a separate file.

 

<?php  

function getImageFromOutside($filePath, $folder, $file)  {  

    if (file_exists($filePath . $folder . $file))
    {
        $contents = file_get_contents($filePath .'/'. $folder .'/'. $file);
        return $contents;
    }

}

header('Content-type: image/jpeg');

$filepath = 'C:/xampp/images/';
$dir = $_GET['d'];
$file = $_GET['f'];

echo getImageFromOutside('C:/xampp/images/', $dir, $file);

?>

 

Then, whenever you want to use the script you would do so like....

 

<html>
  <body>
    <img src="scriptname.php?d=1$f=1.jpg" />
  </body>
</html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.