Page 1 of 1
Serving temporary files
Posted: Tue Aug 14, 2007 1:11 pm
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?
Posted: Tue Aug 14, 2007 3:29 pm
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?
Posted: Tue Aug 14, 2007 3:42 pm
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);
Posted: Tue Aug 14, 2007 3:43 pm
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.'
Posted: Tue Aug 14, 2007 3:46 pm
by GloriousEremite

Ohh, I'm glad I didn't spend too much time on that

Posted: Tue Aug 14, 2007 3:51 pm
by superdezign
GloriousEremite wrote:
Ohh, I'm glad I didn't spend too much time on that

It's a good idea to familiarize yourself with correct header syntax before usage. They are pretty strict.