Solved

Redirect mirror download using PHP

Posted on 2013-06-21
7
504 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 108

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 108

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now