reenadak icon

copy from url

reenadak | PRO | 02/20/18 07:12:25 PM UTC | 0 ⭐ | 418 👁️ | Never ⏰ | []
PHP |

1.26 KB

|

None

|

0 👍

/

0 👎

    
    /*
    - Copy From URL
    - Clones a file from the URL provided and places the copy in your server.
    
    - ! Requires cURL ! -
    
    $url         - The full URL path to the file to copy
    $newLocation - The directory location relative to the location of this script to
                   place the cloned file.
    $newName     - Optional, the name (and the extension!) the cloned file should be
                   renamed to. If left blank the name will stay the same.
    */
    
    public function copyFromUrl($url, $newLocation, $newName = '', $timeOutSecs = 300) {
        $curl = curl_init($url);
        
        if ($newName == '') {
            $oldName = explode("/", $url);
            $newName = $oldName[count($oldName) - 1];
        }
        
        if (substr($newLocation, -1) != '/')
            $newLocation .= '/';
        
        $file = fopen($newLocation . $newName, 'wb');
        curl_setopt($curl, CURLOPT_FILE, $file);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 0); 
        curl_setopt($curl, CURLOPT_TIMEOUT, $timeOutSecs);
        $resp = curl_exec($curl);
        
        if (curl_errno($curl))
            echo "cURL Error: " . curl_error($curl);
        elseif ($resp !== false) {
            curl_close($curl);
            fclose($file);
            return true;
        }
        
        return false;
    }

Comments