Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 558
  • Last Modified:

Redirect mirror download using PHP

I am trying to do a redirect to a file download using PHP. I am having a bit of trouble getting started.

Basically I have 5 files I want to download. Each one has a download button, when the download button is pressed it redirects them to a php page that redirects them to where the file actually lives so they can download it.

I am looking for a similar option that you see in multiple download sites. When you download a file it redirects you to a page that says "Your download will begin soon. If not, click here".
Basically like a mirror.

This also doesn't have to been done in php if there is another alternative.

Any help would be great.
0
askurat1
Asked:
askurat1
1 Solution
 
Jagadishwor DulalBraces MediaCommented:
Basically a button will send fileid or name in another page for download as you explain above. Now if it does not start click here option will then give the file path of server that should work on browser to download.

Again there may be alternative download location if some problem occurs. That will refer to another location like below you can give the file location of different url in href:

<button type="button" name="download" id="<?php echo $fileid;?>">Download</button>

Open in new window


<a href="filepath in your server>Click here to download</a>

Open in new window

0
 
Ray PaseurCommented:
Here is a PHP script that will force a file download.  Just create a a link, point it to the file you want, and you're good to go.

<?php // RAY_force_download.php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('log_errors',     TRUE);


// DEMONSTRATE HOW TO CAUSE A FILE DOWNLOAD


// REQUIRED FOR USE WITH THE PHP date() FUNCTIONS
date_default_timezone_set('America/New_York');

// A FILE TO DOWNLOAD - THIS LINK COULD COME IN THE URL VIA $_GET, OR COULD BE GENERATED INSIDE THE SCRIPT
$url = "http://www.LAPRBass.com/RAY_short_text_file.txt";

// THE USE CASE FOR THE FUNCTION
force_download($url);


// FUNCTION TO FORCE A DOWNLOAD FROM A FILE
function force_download($filename)
{
    // GET THE CONTENTS OF THE FILE
    $filedata = @file_get_contents($filename);

    // SUCCESS
    if ($filedata)
    {
        // GET A NAME FOR THE FILE
        $basename = basename($filename);

        // THESE HEADERS ARE USED ON ALL BROWSERS
        header("Content-Type: application-x/force-download");
        header("Content-Disposition: attachment; filename=$basename");
        header("Content-length: ".(string)(strlen($filedata)));
        header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT");
        header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");

        // THIS HEADER MUST BE OMITTED FOR IE 6+
        if (FALSE === strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE '))
        {
            header("Cache-Control: no-cache, must-revalidate");
        }

        // THIS IS THE LAST HEADER
        header("Pragma: no-cache");

        // FLUSH THE HEADERS TO THE BROWSER
        flush();

        // WRITE THE FILE
        echo $filedata;
    }

    // ERROR
    else
    {
        trigger_error("ERROR: UNABLE TO OPEN $filename", E_USER_ERROR);
    }
}

Open in new window

Best regards, ~Ray
0
 
askurat1Author Commented:
What if the file lives on another server? Specifically Mega.co.nz
Also what if I have multiple download files? Do I need to have a php page for each?
0
 
Ray PaseurCommented:
If you can read the file with file_get_contents() it would not matter what server the file was on.  I am fairly sure you would want to have one clickable link per file, but there is no reason to have separate PHP pages for each file.  The file identifier could be passed to the download page, where you can validate the identifier, and you could thereby limit which files your script will download.
0
 
ZeickanCommented:
For automatic redict in 5 seconds try this:

<META HTTP-EQUIV="REFRESH" CONTENT="5;URL=http://www.mirrorsite.com/file.ext">

Open in new window


For masked mirror download try this:

<?php
// Please, validate me! 
header("location: http://www.mirrorsite.com/".$_GET['file']);
 ?>

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now