How can I save file from external url Laravel public directory How can I save file from external url Laravel public directory laravel laravel

How can I save file from external url Laravel public directory


create an img folder in public path and :

 $image = file_get_contents("https://logos-download.com/wp-content/uploads/2016/09/Laravel_logo.png");file_put_contents(public_path('img/a.png'), $image);


use File like below

   use Illuminate\Support\Facades\File;

and then

File::put(public_path(   'ANY FILE YOU WANT'    ));

example:

 File::put( public_path( 'js/a.js'), ' CONTENT ');


Download files from external URL after authentication.

 public function file_get_contents_curl( $url ) {     $ch = curl_init();       curl_setopt($ch, CURLOPT_HEADER, 0);     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);     curl_setopt($ch, CURLOPT_URL, $url);      $data = curl_exec($ch);     curl_close($ch);     return $data; } public function downloadFile( $imgName, $url, $path ){    $data = $this->file_get_contents_curl( $url );    file_put_contents( $path.$imgName, $data );     echo "File downloaded!";}

Code for creating a new folder based on the date.

   public function imgBackupFolder( $currentShop ) {    $folderName = 'export'.DS.str_replace( '.', '_', $currentShop->shopify_domain ).DS.'images'.DS.date( 'Y_m_d' ).DS;    return $this->createFolder( $folderName );}  /* * Create folder based on the store,date,export type */private function createFolder( $folderName ) {    try {        $path = public_path( $folderName );        if( !File::isDirectory( $path ) ){            File::makeDirectory( $path, 0777, true, true );        }        return $path;    } catch (Exception $ex) {        Log::error( $ex->getMessage() );    }}