I wanted to save a file from External Url which would have any kind of file like say "jpg, png, mp4 etc"
I want to save it in the public directory of Laravel App. How could I do that? Any help will be highly appreciated.
I tried the following thing, but it's saving file in my storage folder:
Storage::put($name, $contents);
Thanks
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() );
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.