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 ); }
 }

No comments:

Post a Comment