Showing posts with label file download. Show all posts
Showing posts with label file download. Show all posts

Monday, 1 August 2016

PHP function to Download File

Download a file from given URL and store it in the specified path. This simple function takes url and path as inputs and saves the file from the url to the specified path.

We can download image files, pdf files, csv files and sql files from the specified url and store in the required path.

function downloadFile($file_url, $save_path) {
            $newfname = $
save_path;
            $file = fopen ($
file_url, 'rb');
            if($file) {
                $newfile = fopen ($newfname, 'wb');
                if($
newfile ) {
                    while(!feof($file)) {
                        fwrite($
newfile , fread($file, 1024 * 8), 1024 * 8);
                    }
                }
            }
            if($file) { fclose($file); }
            if($
newfile ) { fclose($newfile ); }
 }

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.