Skip to content

Instantly share code, notes, and snippets.

@karthikax
Last active December 31, 2022 06:16
Show Gist options
  • Save karthikax/20fc5a5a323ef74e9dd5 to your computer and use it in GitHub Desktop.
Save karthikax/20fc5a5a323ef74e9dd5 to your computer and use it in GitHub Desktop.
PHP Download file to server from URL

PHP Download to Server from Url

Download files from any url to server directly.

  • Contributors: Karthik Bhat
  • Donate link: Official Donate Page
  • Script Platform: PHP
  • License: GPLv2 or later

Description

This script helps developers to download files from any url / other servers directly into their server without needing to download to local computer and then upload to the server.

Installation

  1. Download php-dosu.zip
  2. Extract the zip file.
  3. Inside php-dosu folder 'url-download.php' can be found. Upload it to root your server.
  4. Navigate to "YOURSITE.com/url-download.php"
  5. Enter the remote file url which you want to download directly to your server say "oldserver.com/bigfile.zip"
  6. Click download.

For more information about php-dosu please refer: PHP DoSU Official Page

<html>
<p style="width: 70%;margin: auto;margin-top: 5%;font-size:larger;text-align:center">
Download a file from any URL</p>
<form method="post" style="width: 70%;margin: auto;margin-top: 10%;">
<input name="url" size="50" placeholder="Source URL" style="width: 100%;height: 10%;font-size: 1.5em;padding:10px" required>
<input name="submit" type="submit" value="Download" style="width: 30%;height: 10%;margin: 5% auto; display: block;">
<p style="width: 70%;margin: auto;margin-top: 10%;font-size:larger;text-align:center">
To <?php echo getcwd(); ?></p>
<p style="width: 70%;margin: auto;font-size: smaller;text-align: center;position: fixed;bottom: 0;background: #fff;">
Powered by: <a href="https://karthikbhat.net/portfolio" target="_blank" style="color:#f60;text-decoration:none;">Karthik</a></p>
</form>
<?php
// maximum execution time in seconds
// set_time_limit (24 * 60 * 60);
if (!isset($_POST['submit'])) die();
// folder to save downloaded files to. must end with slash
$destination_folder = '';
$url = $_POST['url'];
$newfname = $destination_folder . basename($url);
/* // old script
$file = fopen ($url, "rb");
if ($file) { $newf = fopen ($newfname, "wb");
if ($newf)
while(!feof($file)) { fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 ); }
}
if ($file) { fclose($file); }
if ($newf) { fclose($newf); }
*/
file_put_contents( $newfname, fopen($url, 'r'));
?>
</html>
@eabbas
Copy link

eabbas commented Jul 2, 2016

سلام خیلی ممنون ازت از این کد استفاده کردم و کارم رو راه انداخت. گرفتن فایل از ربات تلگرام و ذخیره کردن آن در هاست یا سرور

Thank u a lot. it worked out for me. download a file from telegram bot and save it in host is possible with this code:

        $destination_folder = $local_file_path;
    $url = 'https://api.telegram.org/file/bot' . $token . '/' . $file_path;

        $newfname = $destination_folder . basename($url);
        file_put_contents( $newfname, fopen($url, 'r'));   

my telegram id for more information for those who cant solve it for themselves = @Idea14

@saurindashadia
Copy link

This link is not working with above function: https://filehippo.com/download_mozilla-firefox-64/post_download/

Do you know how to download from such links?

@sharpvisions
Copy link

its not working file size is 1.7 Gb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment