Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Redirect mirror download using PHP

Posted on 2013-06-21
7
Medium Priority
?
548 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
5 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 111

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 111

Accepted Solution

by:
Ray Paseur earned 2000 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses

824 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