Laravel Backpack not uploading image - laravel-5

I am attempting to upload a file (country flag) to a simple table countries which should be saved in a "flags" folder of public.
In my add field declaration I have
$this->crud->addField([ // image
'label' => "flag",
'name' => "flag",
'type' => 'image',
'upload' => true,
'disk' => 'flags', // in case you need to show images from a different disk
'prefix' => 'flags/'
and in the filesystems file I have:
'flags' => [
'driver' => 'local',
'root' => public_path('flags'),
'url' => '/flags',
'visibility' => 'public',
],
When I upload it tells me that the field is too short (it is varchar 255) as it seems to want to store the file as data image.

You should take another look at all the instructions for the image field type, in the documentation. Backpack does not take care of the uploading for you - you need an accessor on your Model, so you can choose where it's uploaded and how. If you don't do that, Backpack will try to store it as Base64 in your database - which isn't a good idea in most cases.
Example accessor for flag:
public function setFlagAttribute($value)
{
$attribute_name = "flag";
$disk = "public_folder";
$destination_path = "uploads/folder_1/subfolder_3";
// if the image was erased
if ($value==null) {
// delete the image from disk
\Storage::disk($disk)->delete($this->{$attribute_name});
// set null in the database column
$this->attributes[$attribute_name] = null;
}
// if a base64 was sent, store it in the db
if (starts_with($value, 'data:image'))
{
// 0. Make the image
$image = \Image::make($value)->encode('jpg', 90);
// 1. Generate a filename.
$filename = md5($value.time()).'.jpg';
// 2. Store the image on disk.
\Storage::disk($disk)->put($destination_path.'/'.$filename, $image->stream());
// 3. Save the path to the database
$this->attributes[$attribute_name] = $destination_path.'/'.$filename;
}
}

Related

upload image corrupted using put() on laravel

my code is
$avatarName = $user->id.'_avatar'.time().'.'.request('avatar')->getClientOriginalExtension();
$path = request('avatar')->storeAs('avatars',$avatarName);
Storage::disk('public')->put($avatarName,$path);
and I did on filesystem.php
'public' => [
'driver' => 'local',
'root' => public_path().'/avatars',
'url' => env('APP_URL').'/public',
'visibility' => 'public',
],
image is uploading. But image is corrupted. Original image file size is 1.19MB, after upload image size is 31 Bytes. What should I do?
First you can make sure its a file and a valid file.
Then you can use storeAs :
if($request->hasFile('avatar') && $request->file('avatar')->isValid()){
$avatarName = $user->id.'_avatar'.time().'.'.request('avatar')->getClientOriginalExtension();
// public as the 3rd argument is the disk name to store file in
$file->storeAs('your_path_here', $avatarName, 'public');
}

Upload photo in Laravel

Laravel file upload gives the user error when trying to upload images
Get error: local.ERROR: Driver [] is not supported.
How to fix its problem?
public function channelAvatar(Request $request, Channel $channel)
{
// validate
$this->validate($request, [
'photo' => ['required', 'image', Rule::dimensions()->minWidth(250)->minHeight(250)->ratio(1 / 1)],
]);
// fill variables
$filename = time() . str_random(16) . '.png';
$image = Image::make($request->file('photo')->getRealPath());
$folder = 'channels/avatars';
// crop it
$image = $image->resize(250, 250);
// optimize it
$image->encode('png', 60);
// upload it
Storage::put($folder.'/'.$filename, $image->__toString());
$imageAddress = $this->webAddress() . $folder . '/' . $filename;
// delete the old avatar
Storage::delete('channels/avatars/' . str_after($channel->avatar, 'channels/avatars/'));
// update channel's avatar
$channel->update([
'avatar' => $imageAddress,
]);
$this->putChannelInTheCache($channel);
return $imageAddress;
}
Uploading locally or to FTP still gives the same error.
Whenever you use a Storage option without a specific disk, Laravel uses the default driver. It seems like you have specified local as default driver but you do not have it configured.
As per your config/filesystem.php you have :
'ftp' => [
'driver' => 'ftp',
'host' => env('FTP_HOST', 'test.something.net'),
'username' => env('FTP_USERNAME', 'someusername'),
'password' => env('FTP_PASSWORD', '*******'),
],
So you need to specify this as a default driver. You can do that by adding :
FILESYSTEM_DRIVER=ftp inside the .env file.
And then inside the 'config/filesystem.php` add following :
'default' => env('FILESYSTEM_DRIVER', 'local'),
Now whenever you do Storage::something() it will use default driver. (Something you will have local as the default one`
You can also specify it if you would like :
Storage::disk('ftp')->something() But if your all storage operations use one disk then better specify as default.

How to change upload path to public_html instead of public laravel

i use laravel backpack and i try to upload images. in local working. but in server the image uploaded to public, and not displaying in site i try change disk and in fileSystem but nothing changed
//model
public function setPosterAttribute($value)
{
$attribute_name = "poster";
$disk = "public";
$destination_path = "uploads/products/posters";
// if the image was erased
if ($value==null) {
// delete the image from disk
\Storage::disk($disk)->delete($this->{$attribute_name});
// set null in the database column
$this->attributes[$attribute_name] = null;
}
// if a base64 was sent, store it in the db
if (starts_with($value, 'data:image'))
{
// 0. Make the image
$image = \Image::make($value);
// 1. Generate a filename.
$filename = md5($value.time()).'.jpg';
// 2. Store the image on disk.
\Storage::disk($disk)->put($destination_path.'/'.$filename, $image->stream());
// 3. Save the path to the database
$this->attributes[$attribute_name] = $destination_path.'/'.$filename;
}
}
//fileSystem
'public' => [
'driver' => 'local',
'root' => public_path(),
'visibility' => 'public',
],
Well, in your public array, the root is set to public_path() which is the path to your public folder. If you want to change that, you should should your configuration to the following:
'public' => [
'driver' => 'local',
'root' => base_path('public_html'),
'visibility' => 'public',
],

Wrong path when downloading in Backpack CRUD view

i added an upload field in my CRUD Controller.
Upload works fine and file gets loaded in my /storage/private directory.
Here is filesystems.php file:
'private' => [
'driver' => 'local',
'root' => storage_path('private')
],
Here are my custom functions in the File.php Model:
public static function boot()
{
parent::boot();
static::deleting(function($file) {
\Storage::disk('private')->delete($file->file);
});
}
public function setFileAttribute($value)
{
$attribute_name = "file";
$disk = "private";
$destination_path = "";
// Cifratura del file
file_put_contents($value->getRealPath(), file_get_contents($value->getRealPath()));
$this->uploadFileToDisk($value, $attribute_name, $disk, $destination_path);
}
And here is my FileCRUDController.php code:
$this->crud->addField(
[ // Upload
'name' => 'file',
'label' => 'File to upload',
'type' => 'upload',
'upload' => true,
'disk' => 'private'
]);
When i try to download the file, however, it tries to fetch it from http://localhost:8000/storage/myfile.png instead of http://localhost:8000/storage/private/myfile.png
What i'm doing wrong? Thank you very much.
I would also like to know if there is a way to hook a custom function instead downloading the file directly from the CRUD view. My files are encrypted and i need a controller that cares about decrypting before sending the files to the user.
Method url() is still not usable for the files are placed in subdirectories.
You may also use the storage_path function to generate a fully qualified path to a given file relative to the storage directory:
$app_path = storage_path('app');
$file_path = storage_path('app/file.txt');
In reference to Issue #13610
The following works for version 5.3:
'my-disk' => [
'driver' => 'local',
'root' => storage_path(),
'url' => '/storage'
],
\Storage::disk('my-disk')->url('private/myfile.png')
this should return "/storage/private/myfile.png"

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