Bird
Raised Fist0
Laravelframework~10 mins

Public disk and symbolic links in Laravel - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Public disk and symbolic links
Create storage/app/public folder
↓
Run: php artisan storage:link
↓
Creates symbolic link: public/storage -> storage/app/public
↓
Access files via URL: /storage/filename
↓
Browser serves files from linked folder
This flow shows how Laravel creates a symbolic link from the public folder to storage/app/public, enabling public access to storage files.
Execution Sample
Laravel
php artisan storage:link
// Creates symbolic link
// public/storage -> storage/app/public
// Allows public access to storage files
This command creates a symbolic link so files in storage/app/public are accessible via the public/storage URL.
Execution Table
StepActionResultEffect
1Ensure storage/app/public exists (create manually if needed)Folder existsFolder ready for public files
2Run 'php artisan storage:link'Creates symbolic linkpublic/storage points to storage/app/public
3Verify symbolic linkLink existsFiles in storage/app/public accessible via /storage
4Access file via URL /storage/example.jpgFile served from storage/app/public/example.jpgPublic can view/download file
5Delete symbolic linkLink removedFiles no longer accessible via /storage
💡 Process ends after symbolic link is created and verified for public access
Variable Tracker
VariableStartAfter Step 2After Step 3Final
storage/app/public folderMay not existMay not existExistsExists
public/storage linkDoes not existCreatedExistsExists
File access via /storageFailsFailsSucceedsSucceeds
Key Moments - 3 Insights
Why do we need to run 'php artisan storage:link'?
Because Laravel does not create the symbolic link automatically. Running this command creates the link so files in storage/app/public can be accessed publicly via the web.
What happens if the symbolic link is missing?
Accessing files via /storage URL will fail because the public/storage folder does not point to storage/app/public. See execution_table step 5.
Can we store files outside storage/app/public and access them publicly?
No, only files inside storage/app/public are linked to public/storage. Files outside this folder are not accessible via the symbolic link.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of 'public/storage' link after step 2?
AIt does not exist yet
BIt is deleted
CIt is created and points to storage/app/public
DIt points to a wrong folder
💡 Hint
Check the 'Result' column in execution_table row for step 2
At which step does accessing files via /storage URL become successful?
AStep 3
BStep 1
CStep 5
DNever
💡 Hint
Look at the 'Effect' column in execution_table rows for steps 2 and 3
If the storage/app/public folder does not exist before running the command, what happens?
AThe command fails immediately
BThe link points to a non-existing folder
CThe folder is created and then the link is made
DThe link is created but files are inaccessible
💡 Hint
The storage:link command creates the link but does not create the target folder. See step 1.
Concept Snapshot
Laravel stores public files in storage/app/public.
Run 'php artisan storage:link' to create a symbolic link.
This link is public/storage pointing to storage/app/public.
Files inside storage/app/public become accessible via /storage URL.
Without the link, public access fails.
Keep storage/app/public for public files only.
Full Transcript
In Laravel, files meant for public access are stored in the storage/app/public folder. However, this folder is not directly accessible from the web. To make these files accessible, Laravel uses a symbolic link. Running the command 'php artisan storage:link' creates a symbolic link named public/storage that points to storage/app/public. This means when a user accesses a URL starting with /storage, the server serves files from storage/app/public. The process involves ensuring the storage/app/public folder exists (creating it manually if necessary), running the command to create the link, verifying the link exists, and then accessing files through the public URL. If the symbolic link is missing or deleted, public access to these files will fail. This setup keeps public files organized and accessible securely.

Practice

(1/5)
1. What is the main purpose of the public disk in Laravel's filesystem?
easy
A. To store files that should be accessible publicly via the web
B. To store configuration files for the application
C. To keep temporary cache files hidden from users
D. To store database backups securely

Solution

  1. Step 1: Understand the role of the public disk

    The public disk is designed to hold files that need to be accessed by users through the web, such as images or documents.
  2. Step 2: Compare with other storage uses

    Other disks like local or private are for internal use, not for public access.
  3. Final Answer:

    To store files that should be accessible publicly via the web -> Option A
  4. Quick Check:

    Public disk = public files [OK]
Hint: Public disk means web-accessible files [OK]
Common Mistakes:
  • Confusing public disk with private storage
  • Thinking public disk stores config or cache files
  • Assuming public disk is for backups
2. Which Artisan command creates a symbolic link from public/storage to storage/app/public?
easy
A. php artisan storage:create-link
B. php artisan make:link
C. php artisan link:storage
D. php artisan storage:link

Solution

  1. Step 1: Recall the correct Artisan command

    Laravel uses php artisan storage:link to create the symbolic link for public storage access.
  2. Step 2: Verify other options

    Other commands listed are not valid Laravel Artisan commands for this purpose.
  3. Final Answer:

    php artisan storage:link -> Option D
  4. Quick Check:

    Storage link command = storage:link [OK]
Hint: Use 'storage:link' to create public storage link [OK]
Common Mistakes:
  • Using incorrect Artisan command names
  • Confusing 'make' or 'create' with 'storage:link'
  • Trying to create symbolic links manually without Artisan
3. Given the following code snippet, what will Storage::disk('public')->url('file.txt') return if the symbolic link is correctly created?
use Illuminate\Support\Facades\Storage;

$url = Storage::disk('public')->url('file.txt');
echo $url;
medium
A. storage/app/file.txt
B. http://your-app-url/storage/file.txt
C. /public/storage/file.txt
D. /storage/app/public/file.txt

Solution

  1. Step 1: Understand Storage::url() behavior on public disk

    When the symbolic link is created, Storage::url() returns a URL starting with '/storage/' pointing to the public folder.
  2. Step 2: Confirm the full URL format

    The method returns a full URL including the app's base URL, so it looks like 'http://your-app-url/storage/file.txt'.
  3. Final Answer:

    http://your-app-url/storage/file.txt -> Option B
  4. Quick Check:

    Storage::url() returns public URL [OK]
Hint: Storage::url() returns full public URL [OK]
Common Mistakes:
  • Expecting a file system path instead of URL
  • Confusing storage path with public URL
  • Missing the base URL in the returned string
4. You ran php artisan storage:link but your files in storage/app/public are not accessible via the browser. What is the most likely cause?
medium
A. You forgot to clear the application cache
B. The files are not readable by the web server
C. The symbolic link was not created or points to the wrong folder
D. The public disk is not configured in config/filesystems.php

Solution

  1. Step 1: Check the symbolic link existence and target

    If the symbolic link is missing or points incorrectly, files won't be accessible via public/storage.
  2. Step 2: Consider other causes but focus on link

    While file permissions matter, the most common cause after running the command is a failed or incorrect link creation.
  3. Final Answer:

    The symbolic link was not created or points to the wrong folder -> Option C
  4. Quick Check:

    Broken or missing link = no public access [OK]
Hint: Check symbolic link path if files not accessible [OK]
Common Mistakes:
  • Assuming cache clearing fixes symbolic link issues
  • Ignoring file permissions as a cause
  • Not verifying the link target folder
5. You want to serve user-uploaded images stored in storage/app/public/images via the web. Which steps correctly set this up in Laravel?
hard
A. Run php artisan storage:link and access images via /storage/images/filename.jpg
B. Move images to public/images and access directly without symbolic links
C. Configure a new disk in config/filesystems.php pointing to public/images and use Storage::url()
D. Create a route to read files from storage/app/public/images and return them manually

Solution

  1. Step 1: Create symbolic link for public storage

    Running php artisan storage:link creates a link from public/storage to storage/app/public, making files accessible.
  2. Step 2: Access images via the public URL path

    Files inside storage/app/public/images are then accessible at /storage/images/filename.jpg.
  3. Final Answer:

    Run php artisan storage:link and access images via /storage/images/filename.jpg -> Option A
  4. Quick Check:

    Symbolic link + /storage/ URL = public access [OK]
Hint: Use storage:link and /storage/ URL for public files [OK]
Common Mistakes:
  • Moving files manually instead of linking
  • Not using the symbolic link and expecting direct access
  • Overcomplicating with custom routes unnecessarily
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard