Serving temporary files

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
GloriousEremite
Forum Newbie
Posts: 13
Joined: Tue Aug 14, 2007 1:00 pm

Serving temporary files

Post by GloriousEremite »

I've been searching the internets for a while and I can't seem to find any way to do this.

I'd like to have a script that allows users to upload images. It would modify the image in at least two ways and then return these image files to the visitor of the page. It's easy enough to allow file uploads (maybe not easy to secure it) but how can I ensure that uploaded files are not stored permanently on the server?
GloriousEremite
Forum Newbie
Posts: 13
Joined: Tue Aug 14, 2007 1:00 pm

Post by GloriousEremite »

Ok, I came up with the idea of sending the image to the browser using header("Content-type: image/jpeg") (for jpgs obviously), but it isn't working for some reason. Basically, I'm spawning a new window which should send the jpg to the browser, then delete the image file (not a particularly elegant solution, but I couldn't think of any other way).

Two questions:
1) Is it possible to display html along with the image this way? (I'm fairly sure it isn't, but I'd better ask)
2) Why does this only display the binary data, rather than the image:

Code: Select all

<?php
 header("Content type: image/jpeg");
 header('Content-transfer-encoding: binary');
 header('Content-length: '.filesize($_GET['file']));
 readfile($_GET['file']);
 unlink($_GET['file']);
?>
Let's ignore the security concerns for a moment ;)

I get the same results whether the second two header lines are there, and the same result if the unlink line is omitted (ie, whether I delete the file or not). Am I mad or shouldn't this display an image?
User avatar
TheMoose
Forum Contributor
Posts: 351
Joined: Tue May 23, 2006 10:42 am

Post by TheMoose »

1: No
2: The second header (transfer type) is set to binary mode. Other than that, it should technically work

Try this for images (jpgs only obviously)

Code: Select all

$im = imagecreatefromjpeg($_GET['file']);
imagejpeg($im);
imagedestroy($im);
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

GloriousEremite wrote:1) Is it possible to display html along with the image this way? (I'm fairly sure it isn't, but I'd better ask)
No.
GloriousEremite wrote:2) Why does this only display the binary data, rather than the image:
'Content type' is not 'Content-type.'
GloriousEremite
Forum Newbie
Posts: 13
Joined: Tue Aug 14, 2007 1:00 pm

Post by GloriousEremite »

8O Ohh, I'm glad I didn't spend too much time on that :lol:
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

GloriousEremite wrote:8O Ohh, I'm glad I didn't spend too much time on that :lol:
It's a good idea to familiarize yourself with correct header syntax before usage. They are pretty strict.
Post Reply