Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Apr 2008
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    "Save as" dialog box

    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?

  • #2
    New Coder
    Join Date
    Apr 2008
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts
    So far I have this:

    PHP Code:
    <?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:

    Code:
    <a href="filedownload.php?filename=nameofthefile.txt">Click</a>
    It doesn't work this way. Maybe because I'm using Wordpress. Any idea?

  • #3
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    No its because register globals are off. Something like this would work
    PHP Code:
    <?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 Code:
    <?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 Code:
    <?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($fp4096)); } 
        
    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.
    Last edited by _Aerospace_Eng_; 05-09-2008 at 04:25 PM.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • Users who have thanked _Aerospace_Eng_ for this post:

    joshua7 (05-09-2008)

  • #4
    New Coder
    Join Date
    Apr 2008
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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?

  • #5
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Change this
    PHP Code:
    $realDLfolder "somefolder/"
    to this
    PHP Code:
    $realDLfolder $_SERVER['DOCUMENT_ROOT']; 
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #6
    New Coder
    Join Date
    Apr 2008
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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.

  • #7
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Try this one instead.
    http://www.gentlesource.com/file-download/
    or this one
    http://www.nashruddin.com/pub/posts/...er-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.
    Last edited by _Aerospace_Eng_; 05-11-2008 at 07:39 AM.
    ||||If you are getting paid to do a job, don't ask for help on it!||||


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •