I have the following code to upload and store the file.
$user->update([
'display_profile' => request()->display_profile->storeAs('avatars', $name,'public')
]);
This stores the file in the display_profile as avatars/filename.jpg.
Since I have multiple versions of the files for displaying around the views I am using prefixes like follow
thumb_filename.jpg
small_filename.jpg
large_filename.jpg
I will need to do a lot of string replace to insert the prefix in place to show the right version of the images. Is there anyway I can save just the filename in the database insteat of the full path?
If not whats the best way to show my files in the view?
I use this approach mostly:
$file = $request->file('display_profile');
$name = $file->getClientOriginalName() . '.' . $file->extension();
$file->storeAs('public/avatars', $name);
$user->update([
'display_profile' => $name
]);
When you retrieve a file from the request, this will be converted as an UploadedFile object. You can easily retrieve the original name as follow
$file = $request->display_profile;
$user->update([
'display_profile' => $file->getClientOriginalName()
]);
For your second question I suggest you to create a relation with a Image class where you can store all the image conversion you require. But this can be a little tricky and may require time to be coded. Fortunately there are tons of libraries that can handle this for you. My favourite is Laravel medialibrary
Related
Is there a way which allows you to delete files with a wildcard through the Storage facade in Laravel?
use Illuminate\Support\Facades\Storage;
Storage::disk('foo')->delete('path/TO_DELETE*');
I may have a list like this
path/TO_DELETE_1
path/TO_DELETE_2
path/TO_KEEP_1
path/TO_PROCESS_1
But I want to delete only the files. I want to avoid path manipulation as much as possible
Suppose if you have files start with a_1.jpg,a_2.jpg then
$files = Storage::disk("public")->allFiles();
foreach ($files as $file){
if(Str::startsWith($file,"a_")){
Storage::disk('images')->delete($file);
}
}
or for multiple folder search
$directories = Storage::disk('public')->directories();
foreach ($directories as $directory){
$files= Storage::disk('public')->allFiles($directory);
foreach ($files as $file){
if(Str::startsWith($file,$directory."/"."a_")){
Storage::disk('public')->delete($file);
}
}
}
Another way is using File facade.
File::delete(File::glob(storage_path('app/public/*/a_*.*')));
For example you have multiple folders inside storage/app/pubic/ then you can specify like this.
app/public/*/a_*.*'
Here
First * is any folder inside public folder.
a_* means any file start from a_.
.* is any extension.
For last one i took help from this post
Ref:Delete files with wildcard in Laravel
The sub question here is how to get a list of filtered files in Laravel. This works pretty well:
$fileFilter = "someStringFilenameContains";
$files = collect($fileSource->files())->filter(function ($value) {
return str_contains($value, $fileFilter);
})->toArray();
You can modify str_contains (strstr(), substr() for instance) to suit your needs - anything that returns a truth value really.
This will give you an array of filenames that match your $fileFilter
then to answer the OP:
$fileSource->delete($files);
I have a program that can print an individual PDF when on a students file. I'm doing this using niklasravnsborg/laravel-pdf package in Laravel 5.7. It's working great because I can just stream the pdf from a view into the browser then print from there.
Now I'm wanting to batch print the PDF's at the end of the day instead of one by one. How can I do this? There's no documentation for this on the package repository.
Several ways I've thought of doing this: one, save each PDF as an image file then try to print all files in that folder at the end of the day. If I do this, how would I print all files in that folder?
Next: does anyone know a way to maybe append a new PDF to a variable containing all the previously looped pdf's?
For example:
$finalpdf;
$students = Student::all();
foreach($students as $student){
$pdf = PDF::loadView('pdf.document', $student);
$finalpdf .+ $pdf; //i know this line doesn't work, but how to alter it?
return $pdf->save('document.pdf');
}
After a few days of playing around, hopefully this solution helps someone in the future. I used another package called "lara pdf merger". By writing my files to a save destination, adding those files to a merged file, then deleting the saved files after download, I was able to get my functionality. This is my controller code:
use PDF;
use Illuminate\Filesystem\Filesystem;
use LynX39\LaraPdfMerger\Facades\PdfMerger;
public function batchPrint(){
$pdfMerger = PDFMerger::init();
$i = 0;
$students = Student::whereDate('updated_at', Carbon::today('America/Los_Angeles'))->get();
foreach ($students as $student) {
$pdf = PDF::loadView('printTables', compact('student'));
$pdf->save('pdf/document'.$i.'.pdf');
$pdfMerger->addPDF('pdf/document'.$i.'.pdf');
$i++;
}
$pdfMerger->merge();
$pdfMerger->save("file_name.pdf", "browser");
$file = new Filesystem;
$file->cleanDirectory('pdf');
}
I am trying to add an image to profile in my application. The code returns the correct URL and also the image is stored into the specified folder. But the URL is not saved into the databaseenter image description here
I see two things :
1 - You move your picture with public_path() and you set the $url with URL::to() which is not really logic. Try this way :
$url = public_path() . '/profile_photo/'.$file->getClientOriginalName();
2 - At the end of your if statement, your set an exit() method, which exit from your method. Take it off !
EDIT ----
To save it to your db, do this way :
$user = User::find($id);
$user->profile_photo = $url;
$user->save();
Whenever I use the trans() function to return a specific translation from the validation.php file, it works just perfectly. I have two languages in my application and the translations get returned for both of them.
However, whenever I use the Laravel validator, it returns messages in the default locale only. Is there something I need to specify in the validator? How do I make it work for both languages?
You need to pass as 3rd parameters your translations. Let's assume you have defined your fields, rules and validator like in the following code:
$data = Input::only('title');
$rules['title'] = 'required|min:20|max:80',
$validator = Validator::make($data, $rules,
Lang::get('forms.validation.entry'));
Now you need to define your translations. Let's assume you need translation for fr lang so you need co create lang/fr/forms.php file and put the following content into it:
<?php
return
array (
'validation' => array (
'entry' => array (
'title.required' => 'Your translation for title required',
'title.min' => 'Your translation for title min',
'title.max' => 'Your translation for title max',
)
)
);
Of course you can create file with simpler array but it's just example - instead of forms.validation.entry it could be for example just forms or validation.
The problem originated from my localization implementation. I've added App::setLocale(Session::get('lang')); to the App::before() method in the filters.php file and it all works now.
in resources/lang/en folder laravel have validation.php. When you create new language so you can copy this file to new created language folder and then edit it to this language.
for example you wanna create fr language
.
/resources/lang/fr/validation.php and translate it to this language
I'm creating a feed in CSV file format for products in magento.
We already have some CSV files generated on the fly and returned by magento controller.
But I would like to pre-generate the file and save it in some place and directly return the file upon request.
Where is the best place to save this file?
And how can I return it from controller?
BTW, the file is rarely accessed, so I don't want to store it in memcache, just as a generic file.csv
Creating a directory under {Magento Base Install Path}/var/ would be a logical place to cache the file.
To return the file from the controller you can use:
$fileName = 'file.csv'
$filePath = Mage::getBaseDir('var') . '/csvfilecache/' . $fileName;
$content = array('type' => 'filename', 'value' => $filePath);
$this->_prepareDownloadResponse($fileName, $content, 'text/csv');
See Mage_Core_Controller_Varien_Action::_prepareDownloadResponse