mafortis
mafortis

Reputation: 7128

laravel doesn't upload images on server

I can upload my images while i'm working on local but when I moved my site to host it doesn't upload images while get names of them in database,

my filesystems.php

'default' => env('FILESYSTEM_DRIVER', 'local'),
disks' => [

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

sample of my upload codes:

if ($request->hasFile('imageOne')) {
  $imageOne = $request->file('imageOne');
  $filename = 'productone' . '-' . time() . '.' . $imageOne->getClientOriginalExtension();
  $location = public_path('images/');
  $request->file('imageOne')->move($location, $filename);
  $product->imageOne = $filename;
}

Issue:

In host i get uploaded image name in database, but no image in my public/images folder.

any idea?

UPDATE

I found interesting folder in my host, as I moved all my public folder files to server public_html now it created another folder in my root called public and inside that has images folder including all images i uploaded so far!

Questions:

  1. Why laravel created new public/images folder in my root instead of using my public_html?
  2. How to fix it?

Upvotes: 2

Views: 10959

Answers (2)

TheLastCodeBender
TheLastCodeBender

Reputation: 558

I change the public path inside my save image controller to look like this:

if (request()->imagefile->move(public_path('../../public_html/destination'), $imageName))
     echo "Yap";
else echo "Nop";

Upvotes: -2

mafortis
mafortis

Reputation: 7128

SOLVED

I added this code to my public_html/index.php file and it's working now.

// set the public path to this directory
$app->bind('path.public', function() {
    return __DIR__;
});

Hope it helps.

Upvotes: 6

Related Questions