backpack for laravel segment missing during upload - laravel

I created a CRUD (using backpack crud for laravel )for uploading certificates, when i upload the pdf I use
$this->crud->addField([
'name' => 'respaldo',
'label' => 'Respaldo',
'type' => 'upload',
'upload' => true,
'wrapperAttributes' => [
'class' => 'form-group col-md-6'
],
]);
when i want to see the link of the uploaded file, storage is missing from the route so i added it to a mutator ,see below
public function setRespaldoAttribute($value)
{
$attribute_name = "respaldo";
$disk = "public";
$destination_path = "uploads/respaldos";
$this->uploadFileToDisk($value, $attribute_name, $disk, $destination_path);
}
public function getRespaldoLink() {
return '<a href="'.asset('storage/'.$this->respaldo).'" target="_blank">
Descargar</a>';
}
Now when clicking on the link it's displayed on the listview, the problem is when i click on edit , "storage" segment is missing there so i get 404 instead. see image

I got the same issue and tried to amend the link before displaying it on browser for user to preview in Update or Show CRUD.
The issue is whatever the path is stored inside your database, it will pop up on browser without the head folder. As we know, we can amend the head folder with Laravel LIKE asset('storage/'.$file_path)
So, you need to update the path before storing into database.
Search and Update file HasUploadFields.php
Update this line with add-on 'storage/' Like this:
$this->attributes[$attribute_name] = 'storage/' . $file_path;
Do same for uploadMultipleFilesToDisk method
Now for all new file uploaded, it will add-on 'storage/' before stored file path

Related

Access my files from the public directory

With the upload_multiple field / column, I need to open file with the existing link.
However when I open the link I get a 404.
This is how I do things :
// setupCreateOperation
CRUD::addField([
'name' => 'documents',
'label' => 'Documents',
'type' => 'upload_multiple',
'upload' => true,
]);
// setupShowOperation
CRUD::addColumn([
'name' => 'documents',
'label' => 'Documents',
'type' => 'upload_multiple',
]);
// Model
public function setDocumentsAttribute($value)
{
$attribute_name = "documents";
$disk = "public";
$destination_path = "documents";
$this->uploadMultipleFilesToDisk($value, $attribute_name, $disk, $destination_path);
}
So the documents are in storage > app > public > documents.
What appears in the form (show or edit) : http://localhost/documents/65004a2cdbd104406b025d14c6519ce9.pdf (404)
And I already exectuted php artisan storage:link
If you have any idea, that would be nice...
Thank you!
If you can show your route:
http://localhost/documents/65004a2cdbd104406b025d14c6519ce9.pdf (404)
when you work in localhost with Laravel, you need proporcionate complete url.
Your url to access this resource sould be:
assets('storage/documents/'.65004a2cdbd104406b025d14c6519ce9.pdf)
with this example, try generate link, should can show your documents.
Your problem it´s you need access to storage path.
Sorry for my bad english. I hope help you

Backpack Media Library

I am using laravel backpack.
I want to open media gallery in backpack when click on browse button.
I have added file manager and the images are stored in amazon s3. The images in s3 gallery will open, when click on browse button.
Please help me, how can I open the media gallery?
I have used field type as browse. But, when I select image and trying to save it in amazon s3, it is not saving. Please help me to solve this issue.
My field:
$this->crud->addField([
'label' => "Image",
'name' => "image",
'type' => 'browse',
'upload' => true,
'disk' => 's3',
],
'both');
My model's mutator method:
public function setImageAttribute($value) {
$attribute_name = 'image';
$disk = 's3';
$destination_path = '';
$this->uploadFileToDisk($value, $attribute_name, $disk, $destination_path);
}

Restrict direct access to image url in laravel

If i give image path directly it accessible now. I want image accessible only for if user loged in.
Ex:
This is image path http://localhost:8000/images/test.jpg
If user give this path without loged in through error message as access dined
Can you help me..
There are several ways to fix this problem, the way I would use is to save all images in your resources/assets/ folder outside of the public folder.
Now you could create a route like this:
Route::get('images/{file}', 'ImageController#getImage')->where('file', '.*');
And now in your ImageController, you can check if the user is logged in. If so, you can load the image from the resources folder and send it in a file response. If they are not logged in, there is no way they will be able to access the file.
Tested with Laravel 8
Define a new disk in file: config/filesystem.php
'disks' => [
// ...
'private' => [
'driver' => 'local',
'root' => storage_path('app/private'),
'url' => env('APP_URL').'/storage',
'visibility' => 'private',
],
],
Define web route in file: routes/web.php
Route::any('/private{any}', function (Request $request, $file) {
abort_if(!Storage::disk('private')->exists($file), 404, "File doesn't exist.");
return Storage::disk('private')->response($file);
})->middleware('auth')->where('any', '.*');
Upload image file to folder: storage/private
Test on your localhost at: http://localhost:8000/private/image_name.jpg

How to change storage path in laravel 5.3 to public?

In laravel 5.3, if I upload a file from a form, it will be stored in own storage directory. Then I should create a symlink to that storage path in public path.
I can't use creating symlinks on my system because of the limitation. How can I upload files directly to public/uploads instead of storage/app/uploads?
I tried to set the path in AppServiceProvider.php but it doesn't work.
public function register()
{
//not working:
$this->app->useStoragePath( $this->app->basePath() . "/upload");
//also not working:
// $this->app->bind('path.storage', function () {
// return $this->app->basePath() . '/upload';
// });
}
by "not working" I mean that it is still uploading into the default storage directory. I cleared also the cache.
Here is the upload part (used in a controller):
$request->image->storeAs("uploads", "uploaded_image.jpg");
Thanks for any hints. :)
In Laravel 5 you now have to do this in config/filesystems.php. You can add a new disk like so:
'disks' => [
'uploads' => [
'driver' => 'local',
'root' => public_path(), // previously storage_path();
],
]
and then do the following to use it:
Storage::disk('uploads')->put('filename', $file_content);

Cake PHP Upload plugin - How to store the thumbnail path in the database

I have installed Upload plugin for cakephp.
I've been trying to figure out how to store the thumbnail path or thumbnail name inside a field called image_thumb in my table called Portfolio. I want to store this the same way I can store the original image name. The dir option for the image is there but there is no such thing for thumbnail dir.
My model code where I used upload plugin is this:
public $actsAs = array(
'Upload.Upload' => array(
'image' => array(
'fields' => array(
'dir' => 'image'
),
'thumbnailSizes' => array(
'small' => '640x480',
),
'thumbnailName' => '{filename}_{size}_{geometry}',
'thumbnailPath' => '{ROOT}webroot{DS}img{DS}{model}{DS}Thumbnails{DS}'
)
)
);
Controller, the add function, where the plugin happens I assume (I haven't changed anything here though,
it is the baked code)
public function admin_add() {
$this->layout='admin';
if ($this->request->is('post')) {
$this->Portfolio->create();
if ($this->Portfolio->save($this->request->data)) {
$this->Session->setFlash(__('The portfolio has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The portfolio could not be saved. Please, try again.'));
}
}
}
When the image path is uploaded in database, the image itself and thumbnails are saved in the specified directories in my application. But I want to save the path of the thumbnails inside the database soe that I can use it in my view.
Would really appreciate the help as I'm so new to cakephp and I have to figure it out in a short amount of time.
Thanks.
You may make mistake in here it should be
'fields' => array(
'dir' => 'image_thumb' //table field name
),

Resources