Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Intervention Image will not save the file in a Laravel app

ErrorCan't write image data to path (/var/www/app/public/uploads/photo.jpg)

Short answer

Intervention Image writes straight to a file path. It does not create folders, so the error means the target folder is missing, the web server user cannot write to it, or the path you passed is a URL instead of a filesystem path. Check the folder, then build the path with public_path() or storage_path().

  • Applies to Laravel 12, PHP 8.3, Intervention Image 3, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the exact path you pass to save(), then check that its folder exists and who owns it. The three causes below differ in exactly this output.

    php artisan tinker --execute="echo public_path('uploads');"
    ls -ld /var/www/app/public/uploads
  2. 2Match what you see, then jump to its fix

    1. 1ls says: No such file or directory for the folder, not the fileThe target folder does not existFix
    2. 2The folder exists but is owned by another user, for example root after a manual deployThe web server user cannot write to the folderFix
    3. 3The path in the message starts with http:// or https://, or with a leading slash that points at the site root, not the server diskA URL was passed where a file path is neededFix

What does the error look like?

  • Can't write image data to path (/var/www/app/public/uploads/photo.jpg)

Cause 1 of 3

The target folder does not exist

1Check

Intervention's documentation says the folder structure must already exist and be writable before save() is called.

ls -ld /var/www/app/public/uploads

2Fix

Create the folder once on the server, or create it in code before saving.

use Illuminate\Support\Facades\File;

$dir = public_path('uploads');
File::ensureDirectoryExists($dir);
$image->save($dir.'/photo.jpg');

3Verify

Run the upload again and confirm the file is there.

ls -l /var/www/app/public/uploads

Confirmed by image.intervention.io

Did this fix it?

Cause 2 of 3

The web server user cannot write to the folder

1Check

Laravel's deployment guide says the web server's owner needs write access to storage and bootstrap/cache. A public/uploads folder you added yourself needs the same.

ps -eo user,comm | grep php-fpm | sort -u
ls -ld /var/www/app/storage /var/www/app/public/uploads

2Fix

Give the PHP-FPM user ownership of the folder (the user name here is an example: use the one the first command printed).

sudo chown -R www-data:www-data /var/www/app/public/uploads

3Verify

Upload again and check that the new file is owned by the web server user.

ls -l /var/www/app/public/uploads

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 3 of 3

A URL was passed where a file path is needed

1Check

asset() builds a URL for the browser. public_path() and storage_path() return fully qualified paths on disk, which is what save() needs.

php artisan tinker --execute="echo asset('uploads/photo.jpg').PHP_EOL.public_path('uploads/photo.jpg');"

2Fix

Save with a disk path, and keep asset() only for the link you show in HTML.

$image->save(public_path('uploads/photo.jpg'));   // write
$url = asset('uploads/photo.jpg');                // read in a view

3Verify

The file appears on disk and the URL opens in the browser.

ls -l public/uploads/photo.jpg

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Create upload folders in your deploy script, so they exist before the first request.
  • Run PHP-FPM and the deploy under users that share a group, and keep write access to the folders you use.
  • Use public_path() or storage_path() for every write, and asset() only for links.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Why a Filament panel feels slow on a Laravel server

The panel is fast on a small demo and slow once there are many resources, pages and widgets

A slow Filament panel on a server is most often running without the production caches.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.