Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,880 questions

51,806 answers

573 users

How to download file from URL in PHP

3 Answers

0 votes
$URL = 'https://coupondiscountblog.com/images/xcg.jpg';

file_put_contents("d:\\xcg.jpg", fopen($URL, 'r'));

echo "Done.";

  
/*
run:
          
Done.

*/

 



answered Sep 21, 2019 by avibootz
0 votes
function download_file($url, $local_path) {
    $remote_file = fopen($url, 'rb');
    if ($remote_file) {
        $local_file = fopen($local_path, 'wb');
        if ($local_file) {
            while (!feof($remote_file)) {
                fwrite($local_file, fread($remote_file, 1024 * 10), 1024 * 10);
            }
        }
    }
    if ($remote_file) {
        fclose($remote_file);
    }
    if ($local_file) {
        fclose($local_file);
    }
}



$URL = 'https://coupondiscountblog.com/images/xcg.jpg';
$local_path = "d:\\xcg.jpg";

download_file($URL, $local_path);

echo "Done.";

  
/*
run:
          
Done.

*/

 



answered Sep 21, 2019 by avibootz
edited Sep 21, 2019 by avibootz
0 votes
function get_file_size( $url ) {
    $curl = curl_init( $url );

    curl_setopt($curl, CURLOPT_NOBODY, true);
    curl_setopt($curl, CURLOPT_HEADER, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

    $info = curl_exec($curl);
    curl_close($curl);

    if (preg_match("/^HTTP\/1\.[01] (\d\d\d)/", $info, $matches)) {
        $HTTP_status_code = (int)$matches[1];
    }

    if (preg_match("/Content-Length: (\d+)/", $info, $matches)) {
      $file_size = (int)$matches[1];
    }

    $result = -1;
    if ($HTTP_status_code == 200 || ($HTTP_status_code > 300 && $HTTP_status_code <= 308)) {
      $result = $file_size;
    }
    
    return $result;
}

function download_file($url, $local_path) {
    $remote_file = fopen($url, 'rb');
    if ($remote_file) {
        $local_file = fopen($local_path, 'wb');
        if ($local_file) {
            $file_size = get_file_size($url);
            fwrite($local_file, fread($remote_file, $file_size), $file_size);
        }
    }
    if ($remote_file) {
        fclose($remote_file);
    }
    if ($local_file) {
        fclose($local_file);
    }
}



$url = 'https://coupondiscountblog.com/images/xcg.jpg';
$local_path = "d:\\xcg.jpg";

download_file($url, $local_path);

echo "Done.";


  
/*
run:
          
Done.

*/

 



answered Sep 21, 2019 by avibootz
edited Sep 21, 2019 by avibootz

Related questions

1 answer 223 views
1 answer 99 views
99 views asked Jul 29, 2023 by avibootz
1 answer 241 views
1 answer 155 views
155 views asked May 9, 2016 by avibootz
1 answer 228 views
1 answer 206 views
...