PHP image outside web folder - php

I followed solutions pulled right from this site, and it still won't work!
<?php
$x='/var/image.jpeg';
if(file_exists($x))
{
header('Content-Type: image/jpeg');
readfile($x);
}
?>
And nothing outputs...no error, no anything.
The directory is valid. Without the header file, it just displays text similar to the following
ÿØÿàJFIF``ÿáXExifMM*1>QQQAdobe ImageR
so i presume readfile works. With the header file, nothing gets displayed, just a blank page.
Any clues? Much help appreciated.

use imagejpeg function instead of readfile
and secondly if you have a file stored with you why would you like to display it with php and not with html img src

Related

PHP readfile() not working

I can't get readfile() to work. Trying to display an image. I have this in a page caled 'getImage.php'
<?php
header('Content-Type: image/jpg');
readfile($_SERVER['DOCUMENT_ROOT']."/images/test.jpg");
?>
I've tried using a relative path of just
readfile("images/test.jpg");
I still get a broken image, When viewing the source code the image source shows:
site.co.uk/getImage.php
When I remove the header I get page full of weird symbols and letters.
What am I doing wrong? Could this be a config in the php.ini file? Am on shared hosting, so not sure I can amend it. Have also tried file_get_contents() but still can't display image.
Image size is 242 KB.
Thanks
'When I remove the header I get page full of weird symbols and letters.' It sounds like its working to me. readfile will open the file and read whats inside and display it in the output buffer. Image files are not text files so the weird symbols and letters your seeing is similar to what you would see if you opened the file in a text editor. Where are you trying to display this image in an html file?
Remove the / from front of images and see ? Like this ...
<?php
header('Content-Type: image/jpg');
readfile($_SERVER['DOCUMENT_ROOT']."images/test.jpg");
?>

Image not being displayed from a php file

Following is the piece of code which is not working:-
img src="/old/datagraphsarchive.php?graphing=1&streamCode=ATQ_CALD&date=2008-07-04&iver=1"
Is there any problem with the way I have specified the image source?
This is potentially loading the php file directly instead of as a script. Try using the full external URL in your img src and see if it works.
For example: http://www.yoursite.com/old/datagraphsarchive.php?graphing=1&streamCode=ATQ_CALD&date=2008-07-04&iver=1
If that works, I've yet to actually determine what causes the phenomenon, but you can use the full external url!
If neither work, then your problem lies in the way you're generating your image from the graphsarchive.php file. Ensure you are setting the correct Content-Type headers and writing the binary stream to the page content, like so:
$file = 'myimage.jpg';
header('Content-type: image/jpeg');
readfile($file);

PHP inside img src

Trying to see what actions can be performed with a PHP script that is being called via an image src like so:
<img src="http://example.com/script.php" />
Now, I have tried to include the PHP header() function in script.php:
<?php
header("Location: http://example.com");
I have also tried to echo an image url expecting the img to display it, which it didn't:
<?php
echo 'http://example.com/image.png';
Are there any ways of doing such things with a PHP script that is being called in the img src attribute?
Are there any ways of doing such things with a PHP script that is being called in the img src attribute?
No. A resource that is used as a src for an img tag needs to output image data, nothing else.
There are some exceptions, eg. a header("location: ....") redirect, but the redirect needs to point to another valid image resource, not a web site as you show in your example.
Check out the readfile() as a way to output your image file from your script.php
readfile($file);
Read more about it here in the manual:
http://php.net/manual/en/function.readfile.php
where Example #1 gives an idea of how to set up the headers.
The manual also states that:
readfile() will not present any memory issues, even when sending large
files, on its own.
and
A URL can be used as a filename with this function
ps: This was the way Wordpress Multisite used to open user uploaded (e.g. images) files.
Your script.php should return the output of an image with the correct headers. For instance:
<img src="/html/img/script.php" />
// Script.php
$file = "tiger.jpeg";
$type = "image/jpeg";
header("Content-Type: $type");
header("Content-Length: " . filesize($file));
readfile($file);
You should keep in mind that the src tag should directly point to an image file. However, it is possible to use PHP to create an image, for exmaple by using the GD library:
http://php.net/manual/en/book.image.php
So using:
<img src="http://example.com/script.php" />
is possible, as long as script.php really outputs an image file, for example by using the example as described here:
http://www.php.net/manual/en/image.examples-png.php
I used this kind of processing in the past to overlay texts on JPG images for a broker website (e.g. new, sold, for rent, etc.).
Are there any ways of doing such things with a PHP script that is being called in the img src attribute?
Yes, but the PHP Script has to output image data only, as stated in various other answers.
With that being said, just read the image and output it to the stream with readfile
header('Content-Type: image/png');
readfile($file);
exit();
I know I might be a couple years late to really help you, but the accepted answer just isn't true (anymore).

Display image stored in SQLite3 via PHP Script

I have got an SQLite3 Database with several images in it. I want to display these Images in a Browser (Images are fetched from DB via PHP Script).
header('Content-type: image/jpeg');
$img=$this->getImgData($mid);
//next line is just for testing purposes
file_put_contents("/tmp/thumb.jpg", $img);
echo $img;
exit();
The Problem is:
The Image /tmp/thumb.jpg can be viewed by any image Viewer, but the browser calling this php script (which gets the exact same data as in /tmp/thumb.jpg) refuses to display it.
How could that be?
Make sure not to have any characters written before this section
Most of the time there are spaces before a <?php, or in an include file somewhere above.
Doublecheck that!
Other than that everything look fine. IMO it should work.
Found the Problem:
I copied the content type from a webpage to my IDE.
header('Content-type: image/jpeg');
It seems like the "space" between "Content-type:" and "image/jpeg" wasn't a space but rather any other invisible unicode char. So i just keyed in the header by hand - and it worked!

Broken image with readfile and file_get_contents

I'm trying to use a function in order to get this working:
<img src='login.php?image=ind_legend.jpg'>
But I can't pass through the function to place the image. I went back a couple of steps and tried only this part of the code:
<?php
$file = "http://localhost/sales/test.jpg";
header('Content-type: image/jpeg');
readfile($file);
?>
or using this function:
echo file_get_contents($source);
but the fact is that the only thing I get is a broken image cross (IE) or nothing in Firefox.
I would appreciate any suggestions
Thanks in advance
use the ob_clean() function of php before the readfile()
You certainly have some whitespace in your PHP script, or a UTF-8 BOM invisibly before your <?php opening marker. Use a hexeditor to find out.
To debug it further, open the image URL http://localhost/login.php?image=ind_legend.jpg directly in your browser, save the file. And then compare it to the original JPEG.
As previously mentioned, you probably have some whitespace. I'd try replacing the entire file with the code below. Removing the closing php statement eliminates any chance that there is extra whitespace:
<?php
$file = "http://localhost/sales/test.jpg";
header('Content-type: image/jpeg');
readfile($file);
first of all point your browser to http://youraddress/login.php?image=ind_legend.jpg and check the result.
Maybe the file /sales/test.jpg is corrupted or you don't have enabled the http:// wrapper for readline
At last save the corrupted image via the save image as... context menu option of your browser of choice and try to open it with a text editor. I will not surprised if you will find an error message (if you have them enabled).

Categories