Laravel 5.5 - Transfer file to S3 - laravel

I have an array of filenames that looks like this
array (
'file1.jpg',
'file2.jpg',
'file3.jpg'
)
I am trying to loop through them and upload them to an S3 bucket like this..
foreach ($filenames as $filename) {
Storage::disk('s3')->put(
/* S3 */
's3foldername/' . $foldername . '/' . $filename,
/* Local Storage */
storage_path('localfolder/' . $foldername . '/' . $filename),
'public'
);
}
This isn't working for some reason, the paths all check out ok. Do I need to read the file contents first?

Storage::put() method takes either file content or a resource handle as its second argument - see the docs here: https://laravel.com/docs/5.5/filesystem#storing-files
In your case, the following should do the trick:
Storage::disk('s3')->put(
's3foldername/' . $foldername . '/' . $filename,
file_get_contents(storage_path('localfolder/' . $foldername . '/' . $filename)),
'public'
);
It's just one of the options - see the docs for more.

Related

Laravel 9 Digitalocean spaces upload file

$filename = time() . $file->getClientOriginalName();
Storage::disk('do')->put("public/images/{$directory}/{$filename}", File::get($file), 'public');
return "images/avatars/{$filename}";
I upload file there is not any error but file doesn't upload to digitalocean spaces

Laravel: Calling Artisan command from controller not working properly

I'm trying to backup my db when I hit a spesific route.
first I made artisan command: php artisan backupDB
here is the function:
echo "Generating backup file..\n";
$filename = "backup-" . Carbon::now()->format('Y-m-d-H-i-s') . ".sql";
$command = "mysqldump --user=" . env('DB_USERNAME') ." --password=" . env('DB_PASSWORD') . " --host=" . env('DB_HOST') . " " . env('DB_DATABASE') . " > " . storage_path() . "/app/backup/" . $filename;
$returnVar = NULL;
$output = NULL;
exec($command, $output, $returnVar);
echo "Backup file path: ".storage_path() . "/app/backup/".$filename;
I run it in cli and it is working fine.
then when I tried to run it via controller:
use Illuminate\Support\Facades\Artisan;
public function backupDB{
Artisan::call("backupDB");
}
when I hit the route it prints the echo, but nothing else happened. just that.
So, I found a solution. wrap the command inside a job or just put the code inside that job, and then start the queue. By doing that the code works fine when I try to hit the route.
php artisan make:job backupDB
it will create a file inside app>jobs folder
then put the code inside handle() function.
next just call the job inside your controller or route
use App\Jobs\backupDB;
public function backup()
{
$process = new pull();
dispatch($process);
}
and you need to setup the queue, here is the docs
You don't need to reinvent the wheel. Just use this extension.
PS: I can't explain the problem you are facing, because you didn't provide any debug info: logs, backtrace, or smth like that.
PS2: Be careful with exec() functions usage

Laravel File::deleteDirectory returns true, but directory didn’t actually delete

$cellDirectory = storage_path() . '/app/public/uploads/images/' . $cell->user_id . '/' . $cell->id;
if (is_dir($cellDirectory)) {
$success = File::deleteDirectory($cellDirectory);
}
$success is true, but directory and files didn’t actually delete.
Files in directory have chmod 644
Directory chmod 755
also strange, as the folder was created in this way:
if (!is_dir($path)) {
File::makeDirectory($path, 0777, true);
}
ask for help, thanks
In Linux you cannot delete a non-empty dir unless you specify the argument "-r" (recursive), perhaps it follows the same logic and you have look up the right way to delete a non-empty dir in laravel.

I want to delete the folder after downloading it but after download further code is not executing

$download_path = 'admin_theme' . FOLDER_SEPRATOR . 'temp_folder' . FOLDER_SEPRATOR . $folder_name . '/';
$this->load->library('zip');
$this->zip->read_dir($download_path . '/');
$this->zip->download($folder_name . '.zip');
delete_files($new_folder, true, 1);
unlink($filename);
This will delete the file.
See this manual

Aria2c - how to download a lot of files and ouput as 0.pdf, 1.pdf, 2.pdf ...?

I'm having a little problem here. I'm using the famous Aria2C file downloader and I have a list of links that I need to download. I know I can use:
aria2c -o myOutputFile.exe http://www.fooBar.com/fooBarVirus.exe
to download a single file and name it as myOutputFile.exe. The problem is that I must download some files and name it as 0.pdf, 1.pdf, 2.pdf, 3.pdf... because I will assemble all this pdf's later.
I'll try explain better. If I give the following links to aria2c:
http://www.fooBar.com/myPDF.pdf
http://www.fooBar.com/kindness.pdf
http://www.fooBar.com/crazyPdf.pdf
...
http://www.fooBar.com/myLastPdf.pdf
I must download these files and name it as a sequence of integers FOLLOWING THE LINKS INSERTION ORDER. With that I mean the filenames is related to the links order, but I don't need to download them in order.
Let's suppose I give Aria2C the previous links. The following output would be a valid output for my problem:
Time: 0
Link: http://www.fooBar.com/crazyPdf.pdf
Filename: 2.pdf
Time: 1
Link: http://www.fooBar.com/myPDF.pdf
Filename: 0.pdf
Time: 2
Link: http://www.fooBar.com/kindness.pdf
Filename: 1.pdf
...
Time: N
Link: http://www.fooBar.com/myLastPdf.pdf
Filename: N.pdf
I know this is a confusing question, so if you have any doubt please make a comment and I'll try answer as soon as possible. Thanks!
Ok, let's start with a pdfs.txt file holding the URLs for the PDFs:
http://www.fooBar.com/myPDF.pdf
http://www.fooBar.com/kindness.pdf
http://www.fooBar.com/crazyPdf.pdf
http://www.fooBar.com/myLastPdf.pdf
I throw in a little helper script to turn the URLs from the pdfs.txt file into the file format Aria2c accepts. The script uses the numerical index of an URL in the array as the new filename for the PDF.
<?php
$urls = __DIR__ . '/pdfs.txt';
$downloads = file($urls);
$targetFolder = __DIR__; // define your taget folder
$ariaDownloadList = '';
foreach($downloads as $idx => $url)
{
$pdfName = $idx . '.pdf'; // 0.pdf ... N.pdf
// add new download entry
$ariaDownloadList .= $url;
$ariaDownloadList .= ' dir=' . pathinfo($targetFolder, PATHINFO_DIRNAME) . PHP_EOL;
$ariaDownloadList .= ' out=' . $pdfName . PHP_EOL;
}
file_put_contents('ariaDownloadsFile.txt', $ariaDownloadList);
This outputs a file ariaDownloadsFile.txt with the following content:
http://www.fooBar.com/myPDF.pdf
dir=C:\pdfs
out=0.pdf
http://www.fooBar.com/kindness.pdf
dir=C:\pdfs
out=1.pdf
http://www.fooBar.com/crazyPdf.pdf
dir=C:\pdfs
out=2.pdf
http://www.fooBar.com/myLastPdf.pdf
dir=C:\pdfs
out=3.pdf
Then simply run the following command:
aria2c -i ariaDownloadsFile.txt

Resources