Solved

Redirect mirror download using PHP

Posted on 2013-06-21
7
515 Views
Last Modified: 2013-12-12
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
Comment
Question by:askurat1
7 Comments
 
LVL 15

Expert Comment

by:Jagadishwor Dulal
ID: 39268015
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
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39268090
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
 
LVL 8

Author Comment

by:askurat1
ID: 39268476
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
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39268485
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
 

Expert Comment

by:Zeickan
ID: 39276330
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

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Find out what you should include to make the best professional email signature for your organization.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question