I have a Laravel 9 Forum project and I want to make a upload system manager with Image Intervention package (each uploaded image will be resized into the pre-defined custom sizes).
So I made a table named uploaded_files and it's migrations goes like this:
Schema::create('uploaded_files', function (Blueprint $table) {
$table->id();
$table->bigInteger('upf_object_id');
$table->unsignedBigInteger('upf_object_type_id')->nullable();
$table->string('upf_path');
$table->string('upf_uploaded_as');
$table->string('upf_dimension',15);
$table->string('upf_option')->nullable();
$table->string('upf_type',50);
$table->string('upf_category');
$table->string('upf_mime_type');
$table->string('upf_relation_type',40)->nullable();
$table->string('upf_file_name')->nullable();
$table->string('upf_file_hash')->nullable();
$table->timestamps();
$table->softDeletes();
$table->foreign('upf_object_type_id')->references('id')->on('baseinfos')->onDelete('cascade');
});
Now for uploading images, I put this code at the Controller:
if($request->has('filep')){
$files = $request->file('filep');
foreach ($files as $file) {
$this->uploadImage($file, $questionCreated->id, 'question_image');
}
}
So if the request has an uploaded file, the method uploadImage will be called:
public function uploadImage($request, $id, $type_image)
{
$imagesUrl = UploadFile::upload($request,"Question",$id);
foreach ($imagesUrl['path'] as $path) {
UploadedFile::create([
'upf_object_id' => $id,
'upf_object_type_id' => $this->objectTable('questions'),
'upf_path' => $path['upf_path'],
'upf_uploaded_as' => $type_image,
'upf_dimension' => $path['upf_dimension'],
'upf_type' => $imagesUrl['option']['upf_type'],
'upf_category' => $imagesUrl['option']['upf_category'],
'upf_mime_type' => $imagesUrl['option']['upf_mime_type'],
]);
}
}
And as you can see I have called here a Helper Class which is UploadFile and goes here.
But now the problem is whenever I test this, I get this error:
Can't write image data to path (C:\xampp\htdocs\laravel9project\storage\upload/1401/11/images/questions/51)
Which is referring to the line 117 of the UploadFile class:
->save(storage_path($path));
And I really don't know why this error occurs...
I have also ran the command php artisan storage:link and these links already exists:
'links' => [
public_path('storage') => storage_path('app/public'),
public_path('storage/dist') => storage_path('app/dist'),
public_path('storage/upload') => storage_path('app/upload'),
],
So what's going wrong here? How can I solve this issue?
I would really appreciate any idea or suggestion from you guys about this...
Thanks in advance.
And this is the disks config of filesystems.php (if you want to take a look at):
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
'throw' => false,
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
'throw' => false,
],
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
'url' => env('AWS_URL'),
'endpoint' => env('AWS_ENDPOINT'),
'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false),
'throw' => false,
],
'dist' => [
'driver' => 'local',
'root' => storage_path('app/dist'),
'url' => env('APP_URL') . '/storage/app',
'visibility' => 'public',
'throw' => false,
],
'upload' => [
'driver' => 'local',
'root' => storage_path('app/upload'),
'url' => env('APP_URL') . '/storage/upload',
'visibility' => 'public',
'throw' => false,
],
],
Image Intervention version installed on my project:
"intervention/image": "^2.4",
Related
I have hosted my laravel application, before this file uploads and everything was working fine, but after I put it on cpanel, File uploading is nolonger working.
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
'url' => env('AWS_URL'),
],
],
this is my code, may someone help. in my .env file I inserted my app url for the website which is working file except for file upload
Regards
I am using file system storage to upload files. When I change filesystem_driver from local to public I get this error :
vendor/league/flysystem/src/Adapter/Local.php:367
public function setVisibility($path, $visibility){
$location = $this->applyPathPrefix($path);
$type = is_dir($location) ? 'dir' : 'file';
$success = chmod($location, $this->permissionMap[$type][$visibility]); // line 367
if ($success === false) {
return false;
}
Disk settings
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
'permissions' => [
'file' => [
'public' => 0664,
'private' => 0600,
],
'dir' => [
'public' => 0775,
'private' => 0700,
],
],
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
'url' => env('AWS_URL'),
],
],
I tried to change the file permissions according to this post, but it doesn't work. If I change the filesystem_driver to local I cannot display the pictures in front end. This is a local development environment WSL Ubuntu 18.
What do I do wrong here?
problem was cmod is not support under ntfs file system https://github.com/Microsoft/WSL/issues/81
I have an image upload form in my Laravel project.
I want to save the upload image into public/images/ location.
But image upload into public/app/images location.
From where this app coming !!
config/filesystem.php
'disks' => [
'local' => [
'driver' => 'local',
'root' => public_path(''),
],
'public' => [
'driver' => 'local',
'root' => public_path(''),
'visibility' => 'public',
],
's3' => [
'driver' => 's3',
'key' => 'your-key',
'secret' => 'your-secret',
'region' => 'your-region',
'bucket' => 'your-bucket',
],
],
controller
if($this->request->hasFile('photo'))
{
$image_url = $this->request->photo->store('/images/campaign/large');
$image = substr($image_url, strrpos($image_url, '/') + 1);
}
Where is the mistake ? Anybody help please?
Thanks in advance
I'm using laravel 5.6 and i wanted to create my custom disk for uploading images
and i received this error
InvalidArgumentException Driver [] is not supported.
this is how i save file in controller
$cover = $request->file('cover_image');
$extension = $cover->getClientOriginalExtension();
Storage::disk('test')->put($cover->getFilename().'.'.$extension, File::get($cover));
this is my config/filesystems.php
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'test' => [
'driver' => 'local',
'root' => storage_path(),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public/asghar'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
'url' => env('AWS_URL'),
],
the error occurred because my configuration was cached.
probably I accidentally used config:cache or sth similar.
the issue resolved by clearing the config cache using this command
php artisan config:clear
FileSystem :
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
'shares' => [
'driver' => 'local',
'root' => public_path('shares'),
'visibility' => 'public',
],
's3' => [
'driver' => 's3',
'key' => env('AWS_KEY'),
'secret' => env('AWS_SECRET'),
'region' => env('AWS_REGION'),
'bucket' => env('AWS_BUCKET'),
],
],
My controller :
public function store(Request $request) {
$rules = [
'user_id' => 'required|exists:users,id',
'patient_case_id' => 'required|exists:patient_cases,id',
'shared_with' => 'required|exists:users,id',
];
$this->validate($request, $rules);
$data = $request->all();
$data['shared_file'] = $request->file('file')->store('shares/'.$data["user_id"], $data["shared_file"]->getClientOriginalName());
$newShare = WayneRooney::create($data);
return $this->showOne($newShare, 201);
}
if i leave store method empty, it works and writes file in shares folder in public path with a unique id. But i want to write file with original name under the shares/{USER_ID} folder.
how can i do this ?
If you would not like a file name to be automatically assigned to your stored file, you may use the storeAs method, which receives the path, the file name, and the (optional) disk as its arguments:
$path = $request->file('avatar')->storeAs(
'avatars', $request->user()->id
);
you may also use the putFileAs method on the Storage facade, which will perform the same file manipulation as the example above:
$path = Storage::putFileAs(
'avatars', $request->file('avatar'), $request->user()->id
);
Hope this fixed your issue!