PDA

View Full Version : php download


buggedcom
01-10-2003, 06:25 PM
Hello everyone,

I'm developing a photogallery component that is really too much bloody work to be fun but i have nearly completed it. I am currentley working on using php to forcedownload images rather than having them open them up in browser windows. This is all fine and dandy and I already have a forcedown script... and all is well. What i am trying to do is use an antileaching script with the force download script i already have (below) but any of the scripts i can find i have absolutley no idea how to combine the two as i have no idea about php.

force download script
<?php
$file = $_GET["file"];
download($file);
function download($file, $name = false, $type = false, $down = true) {
if(!file_exists($file)) exit;
if(!$name) $name = basename($file);
if($down) $type = "application/force-download";
else if(!$type) $type = "application/download";
$disp = $down ? "attachment" : "inline";
header("Content-disposition: ".$disp."; filename=$name");
header("Content-length: ".filesize($file));
header("Content-type: ".$type);
header("Connection: close");
header("Expires: 0");
set_time_limit(0 ) ;
readfile($file);
exit;
}
?>


My gallery example page is at http://photogallery.buggedcom.co.uk . Note that although i have the script fully working on my machine i have not yet uploaded it to my server, and the one on the server is not working 100% see the note on the example.

You will also notice that in certain browsers the script causes a window to be opened. I do not want this to happen. I don't know if it can be self closed or there is a script which doesn't do this. Obviously i don't want just to refer the script to be loaded in _self as this will overwrite the flash movie. Anyway and ideas will be greatly appriciated.

Thanks

freddycodes
01-10-2003, 11:09 PM
Here is the one I wrote and use. It uses the No Content 204 header which keeps the new browser window from opening. Feel free to use any part of it. You use readfile() which is fine for jpegs I use fopen because I wanted mine to be able to dl more than just images.


<?php
header("HTTP/1.0 204 No Content");
//Set this to the base of where files
//can be downloaded from for security measures.
$basedir = "D:/wwwroot/";
if(!file_exists($basedir.$_GET['file'])) {
print "Sorry that file does not exist";
exit;
}
else {
header("Content-Type: octet/stream");
header("Content-Disposition: attachment; filename=\"".$_GET['file']."\"");
$fp = fopen($basedir.$_GET['file'], "r");
$data = fread($fp, filesize($basedir.$_GET['file']));
fclose($fp);
print $data;
}
?>

buggedcom
01-10-2003, 11:34 PM
thanks for the reply freddy...

I do not really no php and have been toing a considerable amount of guess work to get my php to work... the way i call the existing php from the swf now is

forcedown.php?file=galleries/australia/ayres%20rock/images/uluru_001.jpg

would this also work for your code? or would i have to add a new var to pass called baseDir. I don't really want to make people have to touch the php so could i send it from the flash also?

freddycodes
01-10-2003, 11:38 PM
Yes you can use


$basedir = $_SERVER["DOCUMENT_ROOT"];