Tuesday 12 July 2016

PHP Function to download a file from URL

Simple php function to download files like pdf, xml, doc and images from a url and save to given path in server
function download_remote($source_url , $destination_path) {
    $file = fopen( $destination_path , 'w+');
    $file_handle = fopen($source_url , "rb");
    while (!feof($file_handle)) {
        $file_contents = fread($file_handle, 8192);
        fwrite($file , $file_contents);
    }
    fclose($file_handle);
    fclose($file);
}
In this $source_url holds the path of file to download and $destination_path holds the path to save file.

No comments:

Post a Comment