Viewed   67 times

I'm displaying images from outside my web root, like this:

header('Content-type:image/png');
readfile($fullpath);

The content-type: image/png is what confuses me.

Someone else helped me out with this code, but I noticed that not all images are PNG. Many are jpg or gif.
And still they are displayed successfully.

does anyone know why?

 Answers

2

The best solution would be to read in the file, then decide which kind of image it is and send out the appropriate header

$filename = basename($file);
$file_extension = strtolower(substr(strrchr($filename,"."),1));

switch( $file_extension ) {
    case "gif": $ctype="image/gif"; break;
    case "png": $ctype="image/png"; break;
    case "jpeg":
    case "jpg": $ctype="image/jpeg"; break;
    case "svg": $ctype="image/svg+xml"; break;
    default:
}

header('Content-type: ' . $ctype);

(Note: the correct content-type for JPG files is image/jpeg)

Saturday, September 17, 2022
2

I'm a bit baffled the warning message doesn't include the location of the code that caused the first content to be sent to the client. The function headers_sent() can return that location, too. So, for debugging purposes, please try

if($delHourExist)
{
  if ( headers_sent($path, $lineno) ) {
    echo '<pre>Debug: output started at ', $path, ':', $lineno, "</pre>n";
  }
  header("location: edit_delivery_hours.php");
}
Thursday, November 3, 2022
 
k_ahir
 
2

First, the quotes in $file = '$var_1'; won't get interpreted correctly,

therefore it needs to read as $file = $var_1;

You also have a missing closing brace }

<?php
if(isset($_GET['link']))
{
    $var_1 = $_GET['link'];
    $file = $var_1;

if (file_exists($file))
    {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
    }
} //- the missing closing brace
?>

And you mentioned that you wanted to use a different folder.

You could use something to the effect of:

$dir = "folder/"; // trailing slash is important
$file = $dir . $var_1;

or

$dir = "../folder/"; // trailing slash is important
$file = $dir . $var_1;

depending on the folder's location.


Edit

The following is tested and worked for me and the files were run from the root of my server.

<?php
if(isset($_GET['link']))
{
    $var_1 = $_GET['link'];
//    $file = $var_1;

$dir = "folder/"; // trailing slash is important
$file = $dir . $var_1;

if (file_exists($file))
    {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
    }
} //- the missing closing brace
?>

HTML (I used a PDF file as an example)

<a href="download.php?link=document.pdf">Download here</a>
Friday, December 9, 2022
 
logan2
 
2

try with this code

$temp = $_FILES["file"]["tmp_name"];
$image = basename($_FILES["file"]["name"]);
$img = "images/".$image;
move_uploaded_file($temp, $img);
echo "<img src=images/".$image' />";
Tuesday, August 2, 2022
 
fabio
 
4
header("Content-type: image/png");

This tells your browser that you're about to pass it raw binary data that is a PNG file. So anything output after that would have to be a binary PNG. You can't then place HTML and expect that to work

echo '<img src="data:image/png;base64,' . base64_encode( $row['image1'] ) . '" />';

This works because you're base64 encoding your image (translates binary into text), outputting that to the browser and then telling your browser to interpret it as base64.

If you want to output the raw binary data you have to rearrange the order. So here's your HTML

<img src="image.php" />

Now you'll note the src points to a PHP file. That's because we're going to have that PHP file return an image. Here's what image.php would look like

//Your query here
$row = mysqli_fetch_assoc($result);
header("Content-type: image/png");
echo $row['image1'];

This works because the browser will call the PHP file, expecting an image. The header tells the browser that this is a PNG file and you can now dump your binary PNG data.

Friday, September 16, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :