Programmmereg
Programmmereg

Reputation: 829

Storage in laravel says symlink - no such file

I deployed laravel app on shared hosting in public_html/app folder. Here is everything from public folder. In /../../files I have rest of files. When I do php artisan storage:link in files folder my console says

[ErrorException]                      
  symlink(): No such file or directory

On localhost I upload files to storage/uploads folder. What to do now? I tried to change links but nothing works for me...

Upvotes: 72

Views: 109044

Answers (24)

MTM
MTM

Reputation: 153

If you deleted public/storage file and has not fixed, yet. Try this composer dump-autoload.

Upvotes: 0

aphoe
aphoe

Reputation: 2726

In my case, I had added more than one symbolic link added to config/filesystems, one of the links is meant to be created in a subfolder but the parent folder doesn't exist ie the link leads to public/users/uploads but public/users folder does not exist.

So the first step was to create public/users folder before running php artisan storage:link

Upvotes: 0

Clemence Ayekple
Clemence Ayekple

Reputation: 116

To fix this error on your server, change directory to the public and remove the symbolic link for the storage folder.

The following commands will help you remove the symbolic link from the public folder:

cd public

rm storage

After removing the symbolic link go back to the root folder using with the command below:

cd ..

Now create the symbolic link with the following command:

php artisan storage:link

After running the command successfully, you should get the following message

The [public/storage] directory has been linked.

I hope this post helps you.

SYMLINK(): No Such File or Directory Laravel

Upvotes: 6

devnc
devnc

Reputation: 100

If someone faces the same error in Docker and Laravel9, try by deleting storage file in public folder and executing php artisan storage:link again. In my case it is 0KB. enter image description here

Upvotes: 0

Chichebem Jibunoh
Chichebem Jibunoh

Reputation: 41

Add this to index.php

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

then on routes/web.php

add this

Route::get('/linkstorage', function () {
    Artisan::call('storage:link');
});

Then run the link

Upvotes: 1

Ambroise
Ambroise

Reputation: 21

Follow these steps:

Step-1: server.php

<?php

/**
 * Laravel - A PHP Framework For Web Artisans
 *
 * @package  Laravel
 * @author   Taylor Otwell <[email protected]>
 */

$uri = urldecode(
    parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
);

// This file allows us to emulate Apache's "mod_rewrite" functionality from the
// built-in PHP web server. This provides a convenient way to test a Laravel
// application without having installed a "real" web server software here.
if ($uri !== '/' && file_exists(__DIR__.'/public_html'.$uri)) {
    return false;
}

require_once __DIR__.'/public_html/index.php';

Step-2: App\Providers\AppServiceProvider.php

/**
 * Register any application services.
 *
 * @return void
 */
public function register()
{
    //Public folder name changed with public_html
    $this->app->bind('path.public', function(){
        return base_path().'/public_html';
    });
}

It is allow you to run the usual command php artisan serve

Step-3: config\filesystems.php

'links' => [
    base_path('public_html/storage') => storage_path('app/public'),
],

To get the storage symbolic link properly with php artisan storage:link

Remember to change the path if is not public_html like for me

Upvotes: 0

Mundruku
Mundruku

Reputation: 339

The issue normally happens when you changed the folder location after you generated your first symbolic link, I solved this isse by following the below steps:

  1. Go in to the public folder and delete the storage folder(It will be labeled with red flag)
  2. After deleting the storage folder, run php artisan storage:link and the issue will be solved

Upvotes: 0

unixproton
unixproton

Reputation: 11

The solution is above but in separate parts, do one and two. Adding the route makes it easier for use case on external servers like shared hosts for example, thanks everyone

  1. Go to config/filesystem.php in the symbolic links section in my case I removed public_path function and wrote my own path in it then run php artisan storage:link command again.
'links' => [
   '/var/www/storage' => storage_path('app/public'),
],
  1. For create storage link without terminal command(get at url)
Route::get('/linkstorage', function () {
    $targetFolder = base_path().'/storage/app/public';
    $linkFolder = $_SERVER['DOCUMENT_ROOT'].'/storage';
    symlink($targetFolder, $linkFolder); 
});

Upvotes: 1

Mehrab HP
Mehrab HP

Reputation: 51

If you are using shared hosting or you have deployed your project on a server with cPanel or DirectAdmin, you probably have a public_html instead of a public folder.

So one thing we usually do is change the Laravel public folder to public_html. and then we go to our AppServiceProvider and bind the new path with the help of code below:

$this->app->bind('path.public', fn() => base_path('/public_html'));

But you should know the configuration files load before these register methods at your providers, so you have also to bind this before your kernel bootstrap your config files.

in order to do that, open index.php and write the code bellow after the $app variable defined:

$app->bind('path.public', fn() => base_path('/public_html'));

I know this might not seem a standard way, but It is working perfectly fine, and I haven't found any critical issue for it yet!

Upvotes: 1

Yogi Arif Widodo
Yogi Arif Widodo

Reputation: 679

more instant i improve the answer of @suresh-pangeni

mv storage storage.bak // just for backup
mkdir -p storage/{app/public,framework/{cache,data,sessions,testing,views},logs}
php artisan storage:link

now its work dont forget to Go to /public directory and run: then rm storage before run on above command.

☺ pa storage:link
production 9999 ✗ The [public/storage] directory has been linked.

Upvotes: 0

CodingEra
CodingEra

Reputation: 1827

Add this inside web.php and then hit this 'BASE_URL/linkstorage' URL once. Now check the storage folder will be created inside the public folder. This will resolve your issue.

Route::get('/linkstorage', function () {
    $targetFolder = base_path().'/storage/app/public';
    $linkFolder = $_SERVER['DOCUMENT_ROOT'].'/storage';
    symlink($targetFolder, $linkFolder); 
});

Upvotes: 4

Suresh Pangeni
Suresh Pangeni

Reputation: 41

if issue is not fixed yet. Do These Steps:

  1. Delete storage link from public folder or run these commands.

a)

cd public

b) rm storage

  1. Goto storage folder and check if app/public folder exists otherwise create new folder.

  2. Check if the error is fixed.

  3. If these steps not fixed your issue delete storage folder and create new storage folder containing all necessary folders on it.

storage folder contains: app, framework,logs

app contains: public framework contains: cache, data,sessions,testing, views

Upvotes: 4

Cody MacLeod
Cody MacLeod

Reputation: 87

In my case, I found that because I had moved all of the /public Laravel files to public_html (on my shared server), that the /public Laravel directory no longer existed.

After making a new "public" directory, I was able to successfully run the 'php artisan storage:link' command.

Upvotes: 0

Andrew Arscott
Andrew Arscott

Reputation: 176

Anyone coming in the future using Laravel Forge. I had this error when I renamed my domain name in Forge.

storage:link error

The solution was to go into the public directory and delete the existing symlink which had been copied over from the original domain.

public symlink to storage

After this symlink had been deleted, I was able to run php artisan storage:link again and this worked correctly.

Upvotes: 8

Gavin
Gavin

Reputation: 874

Just in case your down here still without an answer.

I had trouble because I did the symlink locally, then copied the files to the server. In order to make it work I did the following:

$  cd path/to/laravel/root

// if public does not exist, first create it.
$  cd public

// if public/storage does not exist, first create it.
$  rm -r storage

$  cd ..

// Now it should work
$  php artisan storage:link 

Upvotes: 22

Hesam Moosapour
Hesam Moosapour

Reputation: 592

Go to config/filesystem.php in the symbolic links section in my case I removed public_path function and wrote my own path in it then run php artisan storage:link command again.

 'links' => [
    '/var/www/storage' => storage_path('app/public'),
],

Worked for me.

Upvotes: 3

Arvid de Jong
Arvid de Jong

Reputation: 31

My problem was that te symlink not was placed in /localhost/public/ but in /localhost/storage/app/ When i removed the symlink in /localhost/storage/app/ the artisan command works like a charm

In my code i placed this to prevent creating the wrong symlink

Route::get('/clear', function () {
    Storage::deleteDirectory('public');
    Storage::makeDirectory('public');

    Artisan::call('route:clear');
    Artisan::call('storage:link', [] );
});

Upvotes: 1

Prakash Dahal
Prakash Dahal

Reputation: 540

Follow these three steps:

  1. Remove storage file from public folder.

    rm public/storage

  2. If storage folder is there in your root directory, remove public file inside app folder.

    rm storage/app/public

  3. Run link command

    php artisan storage:link

Upvotes: 4

ali javan
ali javan

Reputation: 101

create new php file and put this code on the file:

<?php
symlink('/home/CpanelUser/storage/app/public', '/home/CpanelUserpublic_html/storage');
?>

"CpanelUser" change to youre cpanel user name or host name.

Upload this file to public_html folder (www) of site execute file with write site name+this file in browser done!

Example: My file name is symlink.php and site name is www.anysite.com upload symlink.php to public_html folder in host and get web address in browser

http://www.anysite.com/symlink.php

after execute this file storage folder creite in public folder in laravel and link to storage folder in app.

Upvotes: 9

ahmedkandil
ahmedkandil

Reputation: 515

Delete the existing storage symbolic link (public/storage) and then run php artisan storage:link command again.

Upvotes: 0

Mahdi Bashirpour
Mahdi Bashirpour

Reputation: 18873

Make sure the APP_URL configuration in .env file is set correctly. after that run following command line:

php artisan config:cache
php artisan storage:link

Upvotes: 3

Al-Amin
Al-Amin

Reputation: 798

I'm face same error when use share hosting and my public directory move to public_html or different directory like : project directory in root name "project" and public directory in root name "public_html"

when run artisan command by Artisan::call('storage:link'); then face this error.

Solution: 1st need to bind your public directory in app/Providers/AppServiceProvider register method

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

Now run the command its working fine

Upvotes: 23

Turan Zamanlı
Turan Zamanlı

Reputation: 3926

  1. Go to /public directory and run:

    rm storage

  2. Go to Laravel root directory and run:

    php artisan storage:link


Edited on May 1st 2018

This problem comes when laravel project is moved/copied to some other folder.

The storage link is still there thus causing the exception error. public/storage folder exists and points to wrong location and it needs to be deleted with rm storage command.

After that run php artisan storage:link in terminal and it will create the storage link.

This needs to be done EVERY time when laravel is moved/copied/deployed!

Upvotes: 244

A Garhy
A Garhy

Reputation: 433

This usually happens after moving Laravel app to another directory

The actual storage directory is "/AppName/storage/app/public/"

Laravel creates a symbolic link pointing to actual storage dir "/AppName/public/storage"

Other meaning

"/AppName/public/storage" points to => "/AppName/storage/app/public/"

On changing root app directory to "/AnotherAppName/" symlink will still point to the old path

"/AnotherAppName/public/storage" => "/AppName/storage/app/public/"

my solution is to manually update the symlink using

ln -sfn /AnotherAppName/storage/app/public/ /AnotherAppName/public/storage

Upvotes: 13

Related Questions