Uses of fpassthru()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Uses of fpassthru()

Post by Benjamin »

I am interested in hearing about the different ways which fpassthru() can be used, and some sample code bits from any of you who have used it in the past.

One question that I have is...

If I have a html page which has an image source tag in it set to be <? getimage.php?show=dog ?>

and then getimage.php had code in similar to this...

<?
if (show == $dog)
{
$dog = "./images/dog.jpg";
fopen $dog;
fpassthru($dog);
fclose $dog;
}

would that work?
Last edited by Benjamin on Fri May 24, 2002 7:43 am, edited 2 times in total.
duke9
Forum Newbie
Posts: 7
Joined: Thu May 23, 2002 4:17 pm

Re: Uses of fpassthru()

Post by duke9 »

agtlewis wrote:

Code: Select all

$dog = ".images/dog.jpg";
fopen $dog;
fpassthru($dog);
fclose $dog;
You need to send the correct MIME type before the file contents, like this

Code: Select all

header("Content-Type: image/jpg");
This forces the browser to interpret the following data as an image
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

So it would look like this?

Code: Select all

<? 
if (show == $dog) 
&#123;
header("Content-Type: image/jpg");
$dog = "./images/dog.jpg"; 
fopen $dog; 
fpassthru($dog); 
fclose $dog; 
&#125;
I didn't know you had to send a Content-Type header but it makes sense.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

I have another question about this...

On a site that has a lot of hits, would this use up a considerable amount of system resources?
Post Reply