My website has a mirror of the download directory in the U.K.. On my download page I have 2 buttons, one for a U.S. download and one for a U.K. download. The current download page passes the filename to the download script. The download script (located in the download directory) for the U.S. download reads as follows:

<?php

$php_scripts = '../../php/';
require $php_scripts . 'PDO_Connection_Select.php';
require $php_scripts . 'GetUserIpAddr.php';
function mydloader($l_filename=NULL)

{
$ip = GetUserIpAddr();
if (!$pdo = PDOConnect("foxclone_data"))
{   
    exit;
}
    if( isset( $l_filename ) ) {  
        header('Content-Type: octet-stream');
        header("Content-Disposition: attachment; filename={$l_filename}");
        header('Pragma: no-cache');
        header('Expires: 0');        
        readfile($l_filename);

        $ext = pathinfo($l_filename, PATHINFO_EXTENSION);
        $stmt = $pdo->prepare("INSERT INTO download (address, filename,ip_address) VALUES (?, ?, inet_aton('$ip'))");
        $stmt->execute([$ip, $ext]) ; 

        $test = $pdo->query("SELECT lookup.id FROM lookup WHERE inet_aton('$ip') >= lookup.ipstart AND inet_aton('$ip') <= lookup.ipend");
        $ref = $test->fetchColumn();
        $ref = intval($ref);

        $stmt = $pdo->prepare("UPDATE download SET lookup_id = '$ref' WHERE address = '$ip'");
        $stmt->execute() ;         
      }

    else {
        echo "isset failed";
        }  
}
mydloader($_GET["f"]);
exit;

How do I modify it to download from the mirrored download directory if they select the U.K. download button. I think the download page will need to point to a separate download script for the U.K. download, but I'm not sure how to code it.

Recommended Answers

All 6 Replies

Additional info: The originating website is https://foxclone.com, the mirror is https://foxclone.org

Would the following work as a separate script for the U.K. download?

<?php
$php_scripts = '../../php/';
require $php_scripts . 'PDO_Connection_Select.php';
require $php_scripts . 'GetUserIpAddr.php';
function mydloader($l_filename=NULL)

{
$ip = GetUserIpAddr();
if (!$pdo = PDOConnect("foxclone_data"))
{   
    exit;
}
    if( isset( $l_filename ) ) {  
       <a href="https://foxclone.org/download/?" download={$l_filename}></a>

        $ext = pathinfo($l_filename, PATHINFO_EXTENSION);
        $stmt = $pdo->prepare("INSERT INTO download (address, filename,ip_address) VALUES (?, ?, inet_aton('$ip'))");
        $stmt->execute([$ip, $ext]) ; 

        $test = $pdo->query("SELECT lookup.id FROM lookup WHERE inet_aton('$ip') >= lookup.ipstart AND inet_aton('$ip') <= lookup.ipend");
        $ref = $test->fetchColumn();
        $ref = intval($ref);

        $stmt = $pdo->prepare("UPDATE download SET lookup_id = '$ref' WHERE address = '$ip'");
        $stmt->execute() ;         
      }

    else {
        echo "isset failed";
        }  
}
mydloader($_GET["f"]);
exit;

TIA

What is the value of $l_filename, and does that server path change depending on the server? If it's a relative path, such as /home/files/image.gif then it's possible that same path is relevant on both US and UK servers.

Yes Dani, the sites are exact duplicates. Paths are the same. The value of $l_filename depends on what they selected to download, it could be xxxxx.iso, xxxxx.deb, xxxxx.src, or xxxxx.pdf

If that’s the case, then why do you need different code at all between the two servers? Just make all URIs and paths be relative so they will work regardless of which server is reached.

Maybe I didn't describe what I'm trying to accomplish clearly. When a user goes to the download page of either site, they have a choice of where to download from for each file, a button to download from the U.S. site and a button to download from the U.K. site. The sites are identical. Maybe an image of what it looks like for one of the files will help.

download.png

So basically what I'm suggesting is have the exact same download PHP code that fetches the local server's copy of the file, across both web servers. One is let's say domain.com and the other is domain.co.uk. On both servers, the download.php script will serve up it's local version of /home/downloads/file.iso.

Then, on that front-end HTML page, simply have:

<a href="https://www.domain.com">US Download</a>
<a href="https://www.domain.co.uk">UK Download</a>

This is also a great use case for the free verison of the Cloudflare CDN, which will host the file across hundreds of datacenters and serve it up wherever it's fastest to the end-user.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.