Jump to content

how do you use readfile() to display a picture?


dbradbury

Recommended Posts

subject really asked my question lol

 

<?php 	
	if(isset($_GET['player']))
	{
		$idpic = mysql_query("SELECT * FROM phpbb_attachments WHERE post_msg_id='$player'");
		$countid = mysql_num_rows($idpic);
		if($countid!=0)
		{
			$row = mysql_fetch_assoc($idpic);
			$file = './forum/files/'.$row['physical_filename'];
			if (file_exists($file)) 
			{
			    header('Content-Description: File Transfer');
			    header('Content-Type: application/octet-stream');
			    header('Content-Disposition: attachment; filename='.basename($file));
			    header('Content-Transfer-Encoding: binary');
			    header('Expires: 0');
			    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
			    header('Pragma: public');
			    header('Content-Length: ' . filesize($file));
			    ob_clean();
			    flush();
			    readfile($file);
			    exit;
			}
		}
	}
?>

 

at the moment it is asking me to download the file...

Link to comment
Share on other sites

nah didnt work, i was trying..

 

<?php 	
if(isset($_GET['player']))
{
	$idpic = mysql_query("SELECT * FROM phpbb_attachments WHERE post_msg_id='$player'");
	$countid = mysql_num_rows($idpic);
	if($countid!=0)
	{
		$row = mysql_fetch_assoc($idpic);
		$file = './forum/files/'.$row['physical_filename'];
		if (file_exists($file)) 
		{
			header('Content-Type: image/jpg');
			echo '<img src='.$file.'';
		}
	}
}
?>

 

but nothing..

Link to comment
Share on other sites

Hi dbradury,

Your modified code is nearly there. Try this:

 

<?php

 

if(isset($_GET['player']))

{

    $idpic = mysql_query("SELECT * FROM phpbb_attachments WHERE post_msg_id='$player'");

    $countid = mysql_num_rows($idpic);

    if($countid!=0)

    {

        $row = mysql_fetch_assoc($idpic);

        $file = './forum/files/'.$row['physical_filename'];

        if (file_exists($file))

        {

            $data = readfile($file);

            header('Content-Type: image/jpg');

            echo $file;

        }

    }

}

 

?>

 

I haven't checked it but it will hopefully send you in the right direction even if it doesn't work.

 

Fergal

Link to comment
Share on other sites

Hi Fergal, thanks for the help btw..

 

i place the code in and uploaded it, the i viewed the page and i got a blank screen, well white background and word image in the centre, with like a box round it..

 

but i took out

header ('Content Type: image/jpg');

 

and view that page again and got this... was weird...

ÿØÿá2äExif and aload of other stuff like that...

 

also it showed my phone make and model, date and time of the picture, when it was taken :\ lol

Link to comment
Share on other sites

no such luck, it did the same thing everytime

 

i know it is a jpg, as i saw the extension when i uploaded it

 

but this is where the pictures are displayed..

2eg8kd1.jpg

 

and as you can see there is no extension on any of the files.. thats because the forum, removes the extension :\ i think

Link to comment
Share on other sites

Sorry, I should have spotted the schoolboy error in the modifed script I gave you (it's been a long day!). It was echoing the path to the file and not the data. Try this on your local file and then the remote one.

 

<?php

 

if(isset($_GET['player']))

{

    $idpic = mysql_query("SELECT * FROM phpbb_attachments WHERE post_msg_id='$player'");

    $countid = mysql_num_rows($idpic);

    if($countid!=0)

    {

        $row = mysql_fetch_assoc($idpic);

        $file = './forum/files/'.$row['physical_filename'];

        if (file_exists($file))

        {

            $data = readfile($file);

            header('Content-Type: image/jpg');

            echo $data;

        }

    }

}

 

?>

Link to comment
Share on other sites

i had to use a script on another page...

 

main page code:

		<?php
	if(isset($_GET['player']))
	{
		$idpic = mysql_query("SELECT * FROM phpbb_attachments WHERE post_msg_id='$player'");
		$countid = mysql_num_rows($idpic);
		if($countid==1)
		{
			$row = mysql_fetch_assoc($idpic);
			if ($row['thumbnail'] =0)
				{
					print '<img src=loadimage.php?image='.$row['physical_filename'].'>';
				}
				else
				{
					print '<img src=loadimage.php?image=thumb_'.$row['physical_filename'].'>';
				}
		}
		else
		{
		echo 'No image yet';
		}
	}		
	?>

 

and my script page:

<?php
$image = $_GET['image'];
header('Content-Type: image/jpeg');
readfile('forum/files/'.$image);
?>

 

and it works perfectly!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.