PHP readfile() not working - php

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");
?>

Related

Why do i get broken image icon even if i set content-type="image/jpeg"?

That question might look silly but i would appreciate if i get a good answer.
I know what http header is and we can change it using header function in php.
Suppose i have a php file an_image.php and the code of it is as below :
<?php header('Content-type:image/jpeg'); ?>
<img src="image/flower.jpg">
Why am i getting a broken icon? By changing the header content type am i not changing the output as image?
As i think img tag is still an html output so as i'm trying to set an html content into an image content so i get the broken icon.
So what is the use of content-type:image/jpeg and where can it be used?
For example flower.jpg picture is in my image folder. If i create flower.php
and open the flower.jpg using a text editor and copy the code of it and paste it on flower.php and set the header content-type:image/jpeg and try to open it on browser it doesn't work saying syntax error.
Looking for a good explanation .
The correct content type for what you're outputting is text/html. You'd use the image/jpeg content type only if you were outputting the actual image file's contents itself.
<?php
header('Content-Type: image/jpeg');
readfile('image/flower.jpg');
Common uses include having a PHP script output a protected file after verifying a user's permissions allow it to be accessed, tracking pixels (save some data then serve a 1x1 image, for example), and serving dynamically generated images.

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 image outside web folder

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

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