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!
Related
Filesystem.php
I have some problem when I try to create a new disk in Laravel 5.2
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'visibility' => 'public',
],
'manifest' => [ // it´s works if only stay this
'driver' => 'local',
'root' => storage_path('../../manifestations_media'),
'visibility' => 'public',
],
'documentos' => [ // if add this nothing works
'driver' => 'local',
'root' => storage_path('../../documentos'),
'visibility' => 'public',
],
's3' => [
'driver' => 's3',
'key' => 'your-key',
'secret' => 'your-secret',
'region' => 'your-region',
'bucket' => 'your-bucket',
],
],
I don't know why it fails, but if I try adding a new disk fails all configuration doesn't work. Can somebody help me understanding why, please?
$mytime = time();
$ext = pathinfo($request->myfile->getClientOriginalName(), PATHINFO_EXTENSION);
$filename = sha1_file($request->file('myfile')->getRealPath()) . $mytime . "." . $ext;
Storage::disk('documentos')->put($filename, file_get_contents($request->file('myfile')->getRealPath()));
DB::update('update cities set document = ? where id = ?', [$filename, $data]);
I solve my problem change the version Laravel 6. This mistake doesn´t. I dont known why happen In Laravel 5.2. It´s possible that i have changed something that i can´t remember, but for the moment it´s works
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 have tried streaming a pdf in the browser but it still tells me i am passing a string as a parameter.
public function cv($id){
$user = new user;
$cv = $user->where('id', $id)->firstOrfail()->Publicprofile->cv_path;
return response()->file(Storage::get($cv));
}
and when i try passing in the name of the file and path it tells me file does not exist
return response()->file($cv);
here is my filesystem config settings
'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'),
],
],
and here is the value the $cv variable returns
"public/cv/61520610986.pdf"
The solution required that I provide an absolute path using the
Public_path() method
ended up doing this:
public function streamCV($id){
$user = new user;
$cv = $user->where('id', $id)->firstOrfail()->Publicprofile->cv_path;
$cv ="storage". ltrim($cv, 'public');
return response()->file(public_path($cv));
}
I'm trying to load a external JSON file into my laravel application. Below you will find a snippet of my code.
$location = __DIR__.'/config.json';
echo $location; // Results in correct file location
echo File::exists($location); // Return 1
echo Storage::get($location); // Throws a FileNotFound Exception
How can the File::exists() method return true and the Storage::get() throw an exception?
My config/filesystems.php file:
<?php
return [
'default' => env('FILESYSTEM_DRIVER', 'local'),
'cloud' => env('FILESYSTEM_CLOUD', 's3'),
'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'),
],
],
];
Why use the Storage class for this? That class is specifically for accessing the storage folder or external file servers. You stored your file right next to your controller/code, so it's probably easier to do it the old-fashioned way.
$location = __DIR__.'/config.json';
if (File::exists($location)) {
$string = file_get_contents($location);
$json = json_decode($string, true);
// logic
} else {
// Do something
}
You can try accessing it through the file facade by
File::get($location);
The error is probably because Storage is trying to access the file from its default path (./project/storage/app) plus what you send it. My best advice, is to add a new disk
like this
// config/filesystems.php
'customDrive' => [
'driver' => 'local',
'root' => base_path(), // here put your full path
],
and acess it through
Storage::disk("customDrive")->get("config.json");
Source https://laravel.com/docs/5.6/filesystem