How to Upload Files in Laravel Directly into Public Folder

How to upload files in Laravel directly into public folder?

You can create a new storage disc in config/filesystems.php:

'public_uploads' => [
'driver' => 'local',
'root' => public_path() . '/uploads',
],

And store files like this:

if(!Storage::disk('public_uploads')->put($path, $file_content)) {
return false;
}

how to store upload files in public folder in laravel 8

You don't need to change any filesystems.php. You upload your video or image anywhere in your project like below

$upload_path = base_path('../');

$path = $request->file($target)->move(

$upload_path.'/uploads/', $fileNameToStore

);

how upload laravel folder (with view files in public folders) to server

  1. Put all files to /public_html / htdocs or other folder when you must upload files
  2. Create .htacces file in this folder
  3. Copy this code to .htaccess file
<IfModule mod_rewrite.c>

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ public/index.php [L,QSA]

</IfModule>

Screen with my publc_html folder structure:

Sample Image

Laravel server, upload to public_html rather than upload in project folder

First, add config to filesystem.php on config folder:

'disks' => [

...

'public_html' => [
'driver' => 'local',
'root' => public_path(),
'visibility' => 'public',
],

...

],

Second, store image with code:

$file->storeAs('images', $filename, 'public_html')

P/S: Remember to configure the correct path for the public_html directory:

$app->bind('path.public', function(){
return __DIR__.'/../../public_html';
});

Laravel Image Upload not saving to public folder

You can use directly the functions provided by Laravel itself

$image_path = Storage::disk('public')->putFile('folders/inside/public', $request->file('post_image'));

Notice Storage::disk('public') that specifies the public folder.

Then you can update your request array with $request['image_path'] = $image_path and save it like you're currently doing or you cant still use your $post = new Post; and set every input data like $post->title = $request->title; then save like $post->save();

Upload image in public folder

try this :

// Validation
$this->validate($request, [
'image' => 'required|image|mimes:jpeg,bmp,png|max:10240' // size in kelobytes this equals 10 mb
]);

$User = new User() ;
$User->password = bcrypt($request->password);
$User->name = $request->name;
$User->type = $request->type;
$User->email = $request->email;
$filename = $this->getFileName($request->image);
$request->image->move(base_path('public/images'), $filename);
$User->image = $fileName;
$$User->save();
return back()->with('success','Image Upload successful');

I just forgot to show you how to implement getFileName function to generate a unique filename sorry about that, in your controller you can define it as protected method like so:

protected function getFileName($file)
{
return str_random(32) . '.' . $file->extension();
}

Laravel 5.5 - Upload to public folder

When you call Storage::put, Laravel will use the default disk which is 'local'.

The local disk stores files at its root: storage_path('app'). The visibility has nothing with where the file should be stored.

You need to choose the public disk which will store the files at its root: storage_path('app/public'),

To do that, you need to tell Laravel which disk to use when uploading the file. Basically change your code to this:

Storage::disk('public')->put($fileName, file_get_contents($file), 'public');



Related Topics



Leave a reply



Submit