Reputation: 53
Having trouble in uploading files in my laravel
project. It's working fine locally but not working on the godaddy
server. I have used the following method to store my file,
First method:
$image = $request->file('avatar_file');
$imageName = time().'.'.request()->avatar_file>getClientOriginalExtension();
$destinationPath = public_path().'/images/' ;
request()->avatar_file->move($destinationPath, $imageName);
Second method:
$image = $request->file('file');
$imageName = time().'.'.request()->file->getClientOriginalExtension();
$request->file('file')->storeAs('documents', $imageName);
I have created symbolic link for this working in local not in server.
Upvotes: 2
Views: 1183
Reputation: 2775
As you have hosted your Laravel app on a shared hosting platform, your app doesn't know the correct path to your public directory.
So, you need to define a function in your public/index.php
file like this: (Paste it on top of your index.php
file)
function public_path($path = '')
{
return realpath(__DIR__)
.($path ? DIRECTORY_SEPARATOR.$path : $path);
}
Defining this function here causes the helper function by the same name to be skipped, thereby allowing its functionality to be overridden. This is required to use a "non-standard" location for Laravel's "public" directory.
Now create a new service provider for your path:
php artisan make:PublicPathServiceProvider
In the register()
function you need the code as:
public function register()
{
if (env('PUBLIC_PATH') !== null) {
//An example that demonstrates setting Laravel's public path.
$this->app['path.public'] = base_path().'/../'.env('PUBLIC_PATH');
} else {
$this->app['path.public'] = base_path().'/../public_html';
}
// Possible environment changes
if ($this->app->environment() === 'local') {
} elseif ($this->app->environment() === 'test') {
} elseif ($this->app->environment() === 'production') {
}
}
Now, register this Provider with your app.
config/app.php
Under Providers add this:
App\Providers\PublicPathServiceProvider::class,
Last step, in your .env
file, create a new variable:
PUBLIC_PATH=/public_html
You are good to go!!!
Note: Do not forget to check the path to where you have uploaded your Laravel app. (Above, it is assumed the path is public_html)
Important: After changing the configurations you might need to clear the cache:
php artisan config:cache
php artisan cache:clear
Upvotes: 2