Skip to content

Tareq Hasan

Simple server to server file downloader script

About a month ago I wrote a script for my own need. It can download files from another server to your server. I just wrote it for my own purpose and it’s pretty simple and stupid enough. But hey, it works ;)

<?php
if (isset($_POST['sub'])) {
    
    if (filter_var($url = $_POST['url'], FILTER_VALIDATE_URL)) {
        define('UPLOAD_DIR', dirname(__FILE__) . '/files/');
        $length   = 5120;

        $handle   = fopen($url, 'rb');
        $filename = UPLOAD_DIR . substr(strrchr($url, '/'), 1);
        $write    = fopen($filename, 'w');

        while (!feof($handle)) {
            $buffer = fread($handle, $length);
            fwrite($write, $buffer);
        }

        fclose($handle);
        fclose($write);
        echo "<h1>File download complete</h1>";
    
    } else {
        echo "<h2>Invalid url</h2>";
    }
}
?>
<form action="" method="POST">
    <p>
        <label for="url">Url:</label>
        <input type="text" size="50" name="url" id="url" />
    </p>
    <p>
        <input type="submit" name="sub" value="download" />
    </p>
</form>

This script has a form input field to make the operation simpler. The downloaded files will be saved on a directory named /files. I am just sharing it, maybe it will help you in anyways :)

14 thoughts on “Simple server to server file downloader script”

  1. When I enter the URL, i am getting the message file download complete but file is not being downloaded. Which path do I need to change in order to see the file?? Thanks