Return a PHP page as an image - php

I am trying to read a image file (.jpeg to be exact), and 'echo' it back to the page output, but have is display an image...
my index.php has an image link like this:
<img src='test.php?image=1234.jpeg' />
and my php script does basically this:
1) read 1234.jpeg
2) echo file contents...
3) I have a feeling I need to return the output back with a mime-type, but this is where I get lost
Once I figure this out, I will be removing the file name input all together and replace it with an image id.
If I am unclear, or you need more information, please reply.

The PHP Manual has this example:
<?php
// open the file in a binary mode
$name = './img/ok.png';
$fp = fopen($name, 'rb');
// send the right headers
header("Content-Type: image/png");
header("Content-Length: " . filesize($name));
// dump the picture and stop the script
fpassthru($fp);
exit;
?>
The important points is that you must send a Content-Type header. Also, you must be careful not include any extra white space (like newlines) in your file before or after the <?php ... ?> tags.
As suggested in the comments, you can avoid the danger of extra white space at the end of your script by omitting the ?> tag:
<?php
$name = './img/ok.png';
$fp = fopen($name, 'rb');
header("Content-Type: image/png");
header("Content-Length: " . filesize($name));
fpassthru($fp);
You still need to carefully avoid white space at the top of the script. One particularly tricky form of white space is a UTF-8 BOM. To avoid that, make sure to save your script as "ANSI" (Notepad) or "ASCII" or "UTF-8 without signature" (Emacs) or similar.

I feel like we can make this code a little bit easier by just getting the mime type from $image_info:
$file_out = "myDirectory/myImage.gif"; // The image to return
if (file_exists($file_out)) {
$image_info = getimagesize($file_out);
//Set the content-type header as appropriate
header('Content-Type: ' . $image_info['mime']);
//Set the content-length header
header('Content-Length: ' . filesize($file_out));
//Write the image bytes to the client
readfile($file_out);
}
else { // Image file not found
header($_SERVER["SERVER_PROTOCOL"] . " 404 Not Found");
}
With this solution any type of image can be processed but it is just another option. Thanks ban-geoengineering for your contribution.

I worked without Content-Length . maybe reason work for remote image files
// open the file in a binary mode
$name = 'https://www.example.com/image_file.jpg';
$fp = fopen($name, 'rb');
// send the right headers
header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
header('Expires: January 01, 2013'); // Date in the past
header('Pragma: no-cache');
header("Content-Type: image/jpg");
/* header("Content-Length: " . filesize($name)); */
// dump the picture and stop the script
fpassthru($fp);
exit;

Very, very easy.
<?php
//could be image/jpeg or image/gif or whatever
header('Content-Type: image/png')
readfile('image.png')
?>

This should work. It may be slower.
$img = imagecreatefromjpeg($filename);
header("Content-Type: image/jpg");
imagejpeg($img);
imagedestroy($img);

Another easy Option (not any better, just different) if you aren't reading from a database is to just use a function to output all the code for you...
Note: If you also wanted php to read the image dimensions and give that to the client for faster rendering, you could easily do that too with this method.
<?php
Function insertImage( $fileName ) {
echo '<img src="path/to/your/images/',$fileName,'">';
}
?>
<html>
<body>
This is my awesome website.<br>
<?php insertImage( '1234.jpg' ); ?><br>
Like my nice picture above?
</body>
</html>

Related

writing not required html to a text file

I am writing my output to the text file and then downloading it using php but issue is that it is saving the output but it is also saving the whole structure of HTML into the textfile also. I don't know why its happening tried to solve it but did'nt figure out how.
I want to save output from fwrite($fh, $formatted_url."\n");
Below is my code:
function get_m3u8_video_segment($url,$portnum=80,$from,$to)
{
$file_name="urlscan.txt";
$fh = fopen($file_name, 'w') or die("Unable to open file!");
for ($x = $from; $x <= $to; $x++)
{
$formatted_url="{$url}:{$portnum}/s-{$x}.m3u8";
//echo "URL is: $formatted_url <br>";
//$contents = file_get_contents($formatted_url);
$contents = get_web_page( $formatted_url );
if ((strpos($contents, 'not found') !== false)||(strpos($contents, 'EXTM3U') !== false))
{
echo" $formatted_url<br>";
fwrite($fh, $formatted_url."\n");
}
}
//header download
header("Content-Disposition: attachment; filename=\"" . $file_name . "\"");
header("Content-Type: application/force-download");
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header("Content-Type: text/plain");
}
get_m3u8_video_segment($url,$portnum,$from,$to);
}
If there is other HTML content elsewhere in your PHP script then this will also be outputted as it normally is, except in this case it will become part of the downloaded file. If you don't want that to happen then you have to stop your script with an exit(); command after you have output the content you actually want. In your script, it looks like you can probably do this just after the call to the function. (But if you have already output some HTML before this, you'll need to alter your script more substantially.)
N.B. I'm surprised you aren't getting a warning about headers being already sent? That normally happens if you try to set headers after you've already echoed some content. Check your log files. Normally you are supposed to output the headers first.
Also, unless you are wanting to keep it for some other purpose, there is no use in saving anything to urlscan.txt - it is not playing any part in your download process. And it would get overwritten every time this script is executed anyway. The headers will cause the browser to treat the output contents (i.e. anything which the PHP script sends to the regular output) as a text file - but this is not the same file as the text file on your server's disk, and its contents can be different.
You happen to be outputting similar content (via echo" $formatted_url<br>";) as you are adding to the urlscan file (via fwrite($fh, $formatted_url."\n");) and I think this may be confusing you into thinking that you're outputting the contents of urlscan.txt, but you aren't - your PHP headers are telling the browser to treat the output of your script (which would normally just go onto the browser window as a HTML page) as a file - but it's a) a new file, and b) actually isn't a file at all until it reaches the browser, it's just a response to a HTTP request. The browser turns it into a file on the client machine because of how it interprets the headers.
Another thing: the content you output needs to be in text format, not HTML, so you need to change the <br> in your echo to a \n.
Lastly, you're outputting the content-type header twice, which is nonsense. A HTTP request or response can only have one content type. In this case, text/plain is the valid MIME type, the other one is not real.
Taking into account all of the above, your code would probably be better written as:
function get_m3u8_video_segment($url, $portnum=80, $from, $to)
{
//header download
$file_name="urlscan.txt";
header("Content-Disposition: attachment; filename=\"" . $file_name . "\"");
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header("Content-Type: text/plain");
for ($x = $from; $x <= $to; $x++)
{
$formatted_url="{$url}:{$portnum}/s-{$x}.m3u8";
$contents = get_web_page( $formatted_url );
if ((strpos($contents, 'not found') !== false)||(strpos($contents, 'EXTM3U') !== false))
{
echo" $formatted_url\n";
}
}
}
get_m3u8_video_segment($url, $portnum, $from, $to);
exit();

How to create a text file through PHP and download the text file in user computer

I have tried several option but the text file saves in root server correctly but I want user to download the text file and save in his computer. I have got the download result by using
header("Content-type: text/plain");
header("Content-Disposition: attachment; filename={$file}");
It downloads the whole script in user computer instead of only "text" result $textToWrite, which I want to download. Code I have tried are as follows:
$textToWrite = "$cdacode"."$cda_name"."$subofcode"."$subofname"."$name_payee"."$acno2"."$ifsc"."$micr_cd"."$act_type"."$pay_amt"."00"."DV NO"."$pmt_ref_no"."$paybydate"."$vendcode"."$vend_add"."$bill_num"."$billdate"."$narration"."$emailid"."$mob_num"."$addn_field";
$file= "$cmpno.txt" ;
$current .= "$textToWrite";
file_put_contents($file, $current);
header("Content-type: text/plain");
header("Content-Disposition: attachment; filename={$file}");
Try this:
$textToWrite = "$cdacode"."$cda_name"."$subofcode"."$subofname"."$name_payee"."$acno2"."$ifsc"."$micr_cd"."$act_type"."$pay_amt"."00"."DV NO"."$pmt_ref_no"."$paybydate"."$vendcode"."$vend_add"."$bill_num"."$billdate"."$narration"."$emailid"."$mob_num"."$addn_field";
$file= "$cmpno.txt" ;
$current .= "$textToWrite";
file_put_contents($file, $current);
header("Content-type: text/plain");
header("Content-Disposition: attachment; filename={$file}");
readfile($file);
As per my comments: as far as I can see, there are two issues with the code:
You have no opening PHP tag, and I wonder if you are just serving this script as text
You're pointlessly writing a file and then doing nothing with it
Try this code:
<?php
// #todo This rather needs tidying up
$textToWrite = "$cdacode"."$cda_name"."$subofcode"."$subofname".
"$name_payee"."$acno2"."$ifsc"."$micr_cd"."$act_type".
"$pay_amt"."00"."DV NO"."$pmt_ref_no"."$paybydate"."$vendcode".
"$vend_add"."$bill_num"."$billdate"."$narration".
"$emailid"."$mob_num"."$addn_field";
$file= "$cmpno.txt";
header("Content-type: text/plain");
header("Content-Disposition: attachment; filename={$file}");
echo $textToWrite;
As per my comments the string is in a bit of a mess - try this first to see if we're on the right track, and if this broadly works, I'll show you how to tidy it up.

PHP readfile() causing corrupt file downloads

I am using php script to provide download from my website after a requisite javascript timer this php script is included which causes the download. But the downloaded file is corrupt no matter whatever I try. Can anyone help me point out where am I going wrong.
This is my code
<?php
include "db.php";
$id = htmlspecialchars($_GET['id']);
$error = false;
$conn = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);
if(!($conn)) echo "Failed To Connect To The Database!";
else{
if(mysql_select_db(DB_NAME,$conn)){
$qry = "SELECT Link FROM downloads WHERE ID=$id";
try{
$result = mysql_query($qry);
if(mysql_num_rows($result)==1){
while($rows = mysql_fetch_array($result)){
$f=$rows['Link'];
}
//pathinfo returns an array of information
$path = pathinfo($f);
//basename say the filename+extension
$n = $path['basename'];
//NOW comes the action, this statement would say that WHATEVER output given by the script is given in form of an octet-stream, or else to make it easy an application or downloadable
header('Content-type: application/octet-stream');
header('Content-Length: ' . filesize($f));
//This would be the one to rename the file
header('Content-Disposition: attachment; filename='.$n.'');
//Finally it reads the file and prepare the output
readfile($f);
exit();
}else $error = true;
}catch(Exception $e){
$error = true;
}
if($error)
{
header("Status: 404 Not Found");
}
}
}
?>
This helped me in case of more output buffers was opened.
//NOW comes the action, this statement would say that WHATEVER output given by the script is given in form of an octet-stream, or else to make it easy an application or downloadable
header('Content-type: application/octet-stream');
header('Content-Length: ' . filesize($f));
//This would be the one to rename the file
header('Content-Disposition: attachment; filename='.$n.'');
//clean all levels of output buffering
while (ob_get_level()) {
ob_end_clean();
}
readfile($f);
exit();
First of all, as some people pointed out on the comments, remove all spaces before the opening PHP tag (<?php) on the first line and that should do the trick (unless this file is included or required by some other file).
When you print anything on the screen, even a single space, your server will send the headers along with the content to be printed (in the case, your blank spaces). To prevent this from happening, you can:
a) not print anything before you're done writing the headers;
b) run an ob_start() as the first thing in your script, write stuff, edit your headers and then ob_flush() and ob_clean() whenever you want your content to be sent to the user's browser.
In b), even if you successfully write your headers without getting an error, the spaces will corrupt your binary file. You should only be writing your binary content, not a few spaces with the binary content.
The ob_ prefix stands for Output Buffer. When calling ob_start(), you tell your application that everything you output (echo, printf, etc) should be held in memory until you explicitly tell it to 'go' (ob_flush()) to the client. That way, you hold the output along with the headers, and when you are done writing them, they will be sent just fine along with the content.
ob_start();//add this to the beginning of your code
if (file_exists($filepath) && is_readable($filepath) ) {
header('Content-Description: File Transfer');
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=$files");
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header("content-length=".filesize($filepath));
header("Content-Transfer-Encoding: binary");
/*add while (ob_get_level()) {
ob_end_clean();
} before readfile()*/
while (ob_get_level()) {
ob_end_clean();
}
flush();
readfile($filepath);

PHP readfile() returning corrupted data

In my function I am saving an image decoded from a base64 string:
function saveImage(){
//magic...
define('UPLOAD_DIR', '../webroot/img/');
$base64string = str_replace('data:image/png;base64,', '', $base64string);
$base64string = str_replace(' ', '+', $base64string);
$data = base64_decode($base64string);
$id = uniqid();
$file = UPLOAD_DIR.$id.'.png';
$success = file_put_contents($file, $data);
}
The above function works properly and the images are saved and not corrupted in the specified folder.
In the next function I am now trying to force download the image to a user:
function getChart($uniqid= null){
if($uniqid){
$this->layout = null;
header("Content-type: image/png");
header("Content-Disposition:attachment;filename='".$uniqid.".png'");
readfile('../webroot/img/'.$uniqid.'.png');
exit;
} else exit;
}
Image downloaded from the server is corrupted and cant be displayed. After opening the downloaded file in a text editor I noticed that a new line character is added at the very top. After deleting the character and saving the file it opens properly and is being displayed properly.
How can I fix this?
What you describe can have multiple issues that are hidden until you actually open the downloaded file.
Instead make your code more robust and check pre-conditions, here if headers have been send already and to clean any possible existing output buffer and give error if that is not possible:
function getChart ($uniqid = null) {
if (!$uniqid) exit;
$this->layout = null;
if (headers_sent()) throw new Exception('Headers sent.');
while (ob_get_level() && ob_end_clean());
if (ob_get_level()) throw new Exception('Buffering is still active.');
header("Content-type: image/png");
header("Content-Disposition:attachment;filename='".$uniqid.".png'");
readfile('../webroot/img/'.$uniqid.'.png');
exit;
}
header("Content-length: $file_size")
This header tells the browser how large the file is. Some browser need it to be able to download the file properly. Anyway it's a good manner telling how big the file is. That way anyone who download the file can predict how long the download will take.
header("Content-type: $file_type")
This header tells the browser what kind of file it tries to download.
header("Content-Disposition: attachment; filename=$file_name");
This tells the browser to save this downloaded file under the specified name. If you don't send this header the browser will try to save the file using the script's name.
BUT you need to flush buffer, with flush(); or in your case with ob_flush(); right above first exit;
check if you are outputing something before calling readfile:
// add ob_start() at the very top of your script
function getChart($uniqid= null){
echo strlen(ob_get_clean()); die(); // if it's not 0 then you are definetly echoing something
if($uniqid){
$this->layout = null;
header("Content-type: image/png");
header("Content-Disposition:attachment;filename='".$uniqid.".png'");
readfile('../webroot/img/'.$uniqid.'.png');
exit;
} else exit;
}
EDIT (to force download):
function getChart($uniqid= null){
if($uniqid){
$image = $uniqid;
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=" . $image);
header("Content-Type: image/jpg");
header("Content-Transfer-Encoding: binary");
readfile($image);
} else exit;
}
getChart("060620121945.jpg");
Try this (just to render image):
function getChart($uniqid= null){
if($uniqid){
$mime_type = "image/png";
$content = file_get_contents('../webroot/img/'.$uniqid.'.png');
$base64 = base64_encode($content);
return ('data:' . $mime_type . ';base64,' . $base64);
} else exit;
}
I have had this problem several times. Here's the solution that I used:
Almost always it turned out that I had forgotten to turn off unicode BOM encoding in my download.php file which adds something to the front of the downloaded file and therefore corrupts it.
So the solution is to turn off unicode BOM in download.php.
Just use ob_clean(); before readfile()

Sending mp3 file through PHP to be used with the audio tag of HTML5 fails in the middle of the mp3

I'm trying to send my mp3 files through a PHP script in order to hide the path to the file in the HTML. Everything works perfectly fine except half way into the mp3 chrome gives me a GET [URL] error. And the rest of the mp3 is just blank. The audio tag thinks it's still reading the file, but there is no sound.
This is how I'm sending the mp3 file from php:
if (file_exists($filename)) {
header("Content-Transfer-Encoding: binary");
header("Content-Type: audio/mpeg");
header('Content-length: ' . filesize($filename));
header('Content-Disposition: inline; filename="' . $filename . '"');
header('X-Pad: avoid browser bug');
header('Cache-Control: no-cache');
readfile($filename);
exit;
}
else {
header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found', true, 404);
echo "no file";
}
Edit: I added the Etag header. It seems to make things better. The problem still occurs but not as often. :-/
Edit2: I have noticed that the request headers sent to my PHP file is different from the headers sent directly to the mp3 file. Actually there's a HTTP_RANGE request sent to the mp3 file, but this range request is missing when I try to fetch things from PHP. (This is in Chrome). Any idea why this might be ?
I know this is an old post but I was able to get this working with two files and index.php and example.php.
this is example.php that's checking a databse for a hash and a used field to know if the file has been used before
<?php
if(isset($_GET['hash'])){
$mysqli = new mysqli('host_here','user_here','password_here','database_here');
$result = $mysqli->query("select * from hashes where hash = '{$_GET['hash']}' limit 1");
$row = $result->fetch_array();
}
if(isset($row['used']) && $row['used'] == 0){
$mysqli->query("update hashes set used=1 where hash = '{$_GET['hash']}'");
$filename = "example.mp3";
header("Content-Transfer-Encoding: binary");
header("Content-Type: audio/mpeg");
header('Content-length: ' . filesize($filename));
//If this is a secret filename then don't include it.
header('Content-Disposition: inline');
//Otherwise you can add it like so, in order to give the download a filename
//header('Content-Disposition: inline;filename='.$filename);
header('Cache-Control: no-cache');
readfile($filename);
exit;
}
elseif(isset($row['used']) && $row['used'] == 1){
die("you can't just download this dude");
}
and here is index.php with the audio tag that will allow playing but not downloading of the mp3.
<html>
<body>
<?php
$mysqli = new mysqli('localhost','root','','test_mp3');
$rand = mt_rand();
$hash = md5($rand);
$result = $mysqli->query("insert into hashes (hash,used) values('$hash',0)");
?>
<audio controls>
<source src="example.php?hash=<?=$hash?>" type="audio/mpeg">
</audio>
</body>
</html>
there is a database table with just a hash and a used field for this example to work

Categories