Display file in browser

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
daven
Forum Contributor
Posts: 332
Joined: Tue Dec 17, 2002 1:29 pm
Location: Gaithersburg, MD
Contact:

Display file in browser

Post by daven »

I am trying to display files in a browser. For example, a php file will merely have the code printed to the browser, rather than interpreted. However, PHP files always are misssing have the line with the initial '<?', and I am having trouble displaying images.
I have also tried using fpassthru, but it will not interpret images at all.

Code: Select all

<?
$dir="/path/on/server/";
$name="filename.ext";
chdir($dir);
header("Content-disposition: inline; filename=$name");
header("Content-type: $mimetype"); # If I comment out this line, text files are interpreted (displayed) correctly, aside from the opening <? line.  However, images are not.

$arr=file($name);
$sca=implode("",$arr);
echo nl2br($sca);
?>
Galahad
Forum Contributor
Posts: 111
Joined: Fri Jun 14, 2002 5:50 pm

Post by Galahad »

Read this thread. My sample code give a pretty good example of what you could do. I had an error in that code The cache-control shouldn't have been public, read through the thread to see some other options. Although, my purpose was to restrict access to files, so maybe public would work for you.

Oh, I just noticed that you replied to that thread... Have you tried the headers I used? When I give it a correct content-type (such as "image/jpeg") it opens in the browser. I'm not sure this is too helpful, but you might get some ideas to try.
Post Reply