How can I save file from external url Laravel public directory
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() ); }}