Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Redirect mirror download using PHP

Posted on 2013-06-21
7
Medium Priority
?
541 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

670 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