...

View Full Version : "Save as" dialog box



joshua7
05-09-2008, 04:37 AM
So far I've tried a few scripts but I can't make it work.
I'd like to open the "save as" dialog box when clicking on a link to save a defined image. Is it hard to make it work in wordpress?

joshua7
05-09-2008, 07:29 AM
So far I have this:


<?php

header("Content-type: application/octet-stream");
header("Content-Length: ".filesize($filename));
header("Content-Disposition: attachment; filename=$filename");
$fp = fopen($filename, 'rb');
fpassthru($fp);
fclose($fp);

?>

I've called this file filedownload.php.
And then I add this link:


<a href="filedownload.php?filename=nameofthefile.txt">Click</a>

It doesn't work this way. Maybe because I'm using Wordpress. Any idea?

_Aerospace_Eng_
05-09-2008, 04:19 PM
No its because register globals are off. Something like this would work

<?php
$filename = $_GET['filename'];
header("Content-type: application/octet-stream");
header("Content-Length: ".filesize($filename));
header("Content-Disposition: attachment; filename=$filename");
$fp = fopen($filename, 'rb');
fpassthru($fp);
fclose($fp);

?>
Though that has no security on what file types can be downloaded. Essentially they could download your login page or config file for your database if they wanted to and then try to hack your site. I've used this script before.

config.inc.php

<?php

// folder that the hidden Downloads files are in
$realDLfolder = "somefolder/";

// approved domains, no http://
$approvedDomains = array();
$approvedDomains[] = "yoursite.com";
$approvedDomains[] = "www.yoursite.com";

// when referrer field is blank or not in array, where should redirect send them?
// I'd recommend just sending them to your main page
$badreferrer = "http://yoursite.com";

?>

filedownload.php

<?php
require "config.inc.php";

$filename = stripslashes($_REQUEST['filename']);
$path = $_REQUEST['path'];

$refr = getenv("HTTP_REFERER");
list($remove,$stuff)=split('//',$refr,2);
list($domain,$stuff)=split('/',$stuff,2);

if(in_array($domain, $approvedDomains)!="1"){
echo "You are not permitted to download that file!<br><a href='".$badreferrer."'>Click here to get a proper link!</a>"; exit; }
else {
if(is_file($realDLfolder.$path.$filename)!==true){
echo "That file can't be found!<br><span style='color:red'>".$path.$filename."</span><br><br><a href='".$badreferrer."'>Click here to get a proper link!</a>"; exit; }
else {
// File exists, referrer checks out, and file is accessible to webserver... Let's download!
$filesize = filesize($realDLfolder.$path.$filename);
if (ereg(".mp3",$filename)){$xtype="audio/mpeg";}
elseif(ereg(".zip",$filename)){$xtype="application/x-zip-compressed";}
elseif(ereg(".exe",$filename)){$xtype="application/octet-stream";}
elseif(ereg(".txt",$filename)){$xtype="text/plain charset='us-ascii'";}
elseif(ereg(".doc",$filename)){$xtype="application/msword";}
elseif(ereg(".xls",$filename)){$xtype="application/vnd.ms-excel";}
elseif(ereg(".ppt",$filename)){$xtype="application/vnd.ms-powerpoint";}
elseif(ereg(".gif",$filename)){$xtype="image/gif";}
elseif(ereg(".png",$filename)){$xtype="image/png";}
elseif(ereg(".jpg",$filename)){$xtype="image/jpg";}
elseif(ereg(".wav",$filename)){$xtype="audio/x-wav";}
elseif(ereg(".mpe",$filename)){$xtype="video/mpeg";}
elseif(ereg(".mov",$filename)){$xtype="video/quicktime";}
elseif(ereg(".avi",$filename)){$xtype="video/x-msvideo";}
else { $xtype="application/force-download"; }

$fp=fopen($realDLfolder.$path.$filename,"rb");
if ($fp) {
// Create the headers used for downloading the file
header("Content-Transfer-Encoding: binary");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: ".$xtype);
header("Accept-Ranges: bytes");
// Actually start downloading the file
while (!feof($fp)) {
echo(@fgets($fp, 4096)); }
fclose ($fp);
}
else {
echo "There was a problem downloading that file!<br><span style'color:red'>".$path.$filename."</span><br><br><a href='".$badreferrer."'>Click here to get a proper link!</a>"; exit; }
}
}
?>
Be sure to edit the allowed file extensions to suit your needs.

joshua7
05-09-2008, 06:41 PM
wow awesome
Your script is so complete.
Thanks so much for helping me out.

The folders where I'll keep the files are automatically generated by date. Is there a way to download from my site's url instead of a folder?

_Aerospace_Eng_
05-09-2008, 07:19 PM
Change this

$realDLfolder = "somefolder/";
to this

$realDLfolder = $_SERVER['DOCUMENT_ROOT'];

joshua7
05-10-2008, 10:17 AM
I'm having some issues.
Filedownload.php and config.inc.php are in the same folder the images are in but when I call the file I get "That file can't be found". I've changed $realDLfolder to just a slash or the name of the folder the images are in with no luck.
Your first script, the short one works perfect but i'd like to use the one you recommended.

_Aerospace_Eng_
05-11-2008, 07:37 AM
Try this one instead.
http://www.gentlesource.com/file-download/
or this one
http://www.nashruddin.com/pub/posts/simple-file-server-and-downloader-script.html

There are plenty of scripts like this out there on the net many of them free. www.hotscripts.com has a lot of to choose from. Just search for file download in the php section.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum