download file from another server in php

Solutions on MaxInterview for download file from another server in php by the best coders in the world

showing results for - "download file from another server in php"
Isabella
17 Oct 2020
1<?php
2    $curl = curl_init();
3
4    // URL to Download File
5    $url = "https://wallpapercave.com/wp/wp2663986.png";    
6    
7    // Get File name from URL ( Last string from url)
8    $string = explode('/', $url);
9    $last_word = array_pop($string);    
10
11    // Download Path 
12    $path = 'images/' . $last_word;  // images folder must already exist.
13    $myfile = fopen( $path , 'w+');
14
15    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
16    curl_setopt($curl, CURLOPT_URL, $url);
17    curl_setopt($curl, CURLOPT_FILE, $myfile);
18
19    $data = curl_exec($curl);
20    
21    if ($data)
22      {
23        echo "Download Completed <br>";
24        echo "Output :  $path <br>";
25        echo "File Name :  $last_word <br>";
26        echo "URL :  $url <br>" ;
27      }
28    $err = curl_error($curl);
29    echo $err;
30    curl_close($curl); 
31 ?>