Laravel uploaded file not appears in linked "public/storage" folder - php

When I run command: php artisan storage:link, this creates folder /public/storage.
then I have code, which handles uploaded file:
// get file original name and extension here, then generate file new name $fileNameToStore
// set file path
$path = $request->file('my_file')->storeAs('public/uploaded_imgs', $fileNameToStore);
Code works and uploaded file appears in /storage/app/public/uploaded_imgs/ folder, which is nice, though there is nothing in /public/storage folder.
Why there is not uploaded_imgs folder in /public/storage directory? What I'm doing wrong?

In config/filesystems.php, you could do this... change the root element in public
Note : instead of upload you can use your folder name
'disks' => [
'public' => [
'driver' => 'local',
'root' => public_path() . '/uploads',
'url' => env('APP_URL').'/public',
'visibility' => 'public',
]
]
and you can access it by
Storage::disk('public')->put('uploaded_imgs', $request->file('my_file'));
or
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path(),
],
'uploads' => [
'driver' => 'local',
'root' => public_path() . '/uploads',
],
]
Then use it :
Storage::disk('uploads')->put('filename', $file_content);

Try this:
Storage::disk('public')->put('uploaded_imgs', $request->file('my_file'));
hopefully it will work.

When you run php artisan storage:link command, Laravel generates a "storage" symlink under "public" folder which directs to /storage/app/public so your code is correct.
There is no public/storage directory, its a symlink which directs to /storage/app/public which is where your uploaded_imgs folder has been generated.
public/storage => /storage/app/public

You can use the following code to upload a file:
$file = $request->file('my_file');
$fileNameWithoutExtension = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
$path = "public/uploaded_imgs/" + $fileNameWithoutExtension +"."+$file->getClientOriginalExtension();
Storage::disk('local')->put($path, file_get_contents($file), 'public');

Related

How to upload file to remote server in public web directory using sftp in Laravel 8

I have managed to configure my sftp in the 'config/filesystems.php' file:
'remote-sftp' => [
'driver' => 'sftp',
'host' => env('SFTP_HOST'),
'username' => env('SFTP_USERNAME'),
'password' => env('SFTP_PASSWORD'),
'root' => '/var/www/html/files/current',
'visibility' => 'public',
'permPublic' => 0644,
'timeout' => 30,
],
An sftp account was created on the remote server (usert) and I successfully manage to upload the file to the server, but, the problem is that the files is being stored in the wrong folder. My code to upload the file :
$fileName = $id_number . '_tps_' . $datetime . '.' . $request->file('file')->extension();
Storage::disk('remote-sftp')->put($fileName, fopen($request->file('file'), 'r+'), 'public');
I expect the file to be stored in '/var/www/html/files/current' as specified in 'config/filesystems.php' but i find the file is actually stored in '/home/usert'. How to I get it to save in the '/var/www/html/files/current'?

Disk [public] does not have a configured driver. in laravel image upload

I am trying to upload a file to a public folder which was working lately but now it is showing below error:
Disk [public] does not have a configured driver.
I tried checking for configured driver in config/filesystems.php but, it is already set there. I am not getting where the issue might be.
Upload code:
public function upload(ProductImageRequest $request, Product $product)
{
$image = $request->file('file');
$dbPath = $image->storePublicly('uploads/catalog/'.$product->id, 'public');
if ($product->images === null || $product->images->count() === 0) {
$imageModel = $product->images()->create(
['path' => $dbPath,
'is_main_image' => 1, ]
);
} else {
$imageModel = $product->images()->create(['path' => $dbPath]);
}
return response()->json(['image' => $imageModel]);
}
Code in config/filesystems.php
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
i use this code for moving the picture and storing its name you may want to give it a shot
//get icon path and moving it
$iconName = time().'.'.request()->icon->getClientOriginalExtension();
$icon_path = '/category/icon/'.$iconName;
request()->icon->move(public_path('/category/icon/'), $iconName);
$category->icon = $icon_path;
i usually move the image then store its path in db and this is what my code shows you can edit it as desired

Laravel - Can't save files to public_path using storeAs

I cannot upload files to the public_path folder in Laravel 5.4. I can't understand what's going wrong, the documentation makes it look easy. $request is the POSTed contents of a form. filename is a file submitted via the form.
public function uploadFile($request) {
if ($request->hasFile('filename') && $request->file('filename')->isValid()) {
$file = $request->filename;
$hash = uniqid(rand(10000,99999), true);
$directory = public_path('files/'.$hash);
if(File::makeDirectory($directory, 0775, true)) {
return $file->storeAs($directory, $file->getClientOriginalName());
}
}
return NULL;
}
The directory is created, but there's no file inside. As you can see, the folder has 775 permissions.
I've tried added a trailing slash. I've tried removing public_path altogether. Nothing works.
What am I doing wrong? :(
By default file system use your default disk named 'local' that upload files in storage/app folder store using store, stroeAs etc...
The filesystem configuration file is located at config/filesystems.php.
either you can change root path under 'local'
from 'root' => storage_path('app'), to 'root' => public_path('files'),
and then in your code change from
$directory = public_path('files/'.$hash); to $directory = public_path($hash);
OR you can create new disk in config/filesystem.php
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'my_upload' => [
'driver' => 'local',
'root' => public_path('files'),
'visibility' => 'public',
],
and then mention new disk as below while storing file
$file->storeAs($directory, $file->getClientOriginalName(), 'my_upload');
After performing all above if not work hit below commands in order
php artisan config:clear
php artisan cache:clear
php artisan config:cache
You can try this :
if(File::makeDirectory($directory, 0775, true)) {
return $file->store($directory, $file->getClientOriginalName());
}
Hope this help you !

Laravel 5.2 uploading file using filesystem

I am having some troubles with laravels filesystem uploading.
When I try to execute this code
Storage::disk('public')->put(
$img->getClientOriginalName(),
file_get_contents($img->getRealPath())
);
nothing happens locally in the public folder, I even checked if the file exists and it returns true
dd(Storage::disk('public')->exists($img->getClientOriginalName()));
For now I am using the $img->move method and it works as I want to.
Disk is also configured in filesystems.php
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'visibility' => 'public',
],
I am confused with this because a couple of weeks ago it worked as it should on another project.
I have now fixed this problem with the help of Claudio by using 'root' => public_path('').
This should work:
if ($request->hasFile('myFile'))
{
$fileExtension = strtolower($request->file('myFile')->getClientOriginalExtension());
$newFilename = str_random(20) . '.' . $fileExtension;
$storagePath = storage_path() . '/app/uploads/';
$request->file('myFile')->move($storagePath, $newFilename);
}

Unabled to upload load "file", permission denied

i use elfinder to upload file throught FTP driver, but i can not upload file to server, elfinder show messagers dialog "unabled to upload file, permission denied". I have take owership for the user use FTP but not affected:(, please help me
$newname should be full path to a new file. But in your script it is a relative path that seems to point somewhere inside /tmp
I solved this problem by adding extra configurations to elfinder root. For example:
$root = array(
// -------------------------------
'driver' => 'FTP',
'host' => 'ftp_ip',
'user' => 'username',
'pass' => 'password',
// -------------------------------
'port' => 21,
'mode' => 'passive',
'alias' => 'folder_alias,
'owner' => true,
'tmbPath' => '/tmp',
'tmpPath' => '/tmp',
'dirMode' => 0777,
'fileMode' => 0777
);

Categories