Intervention Image will not save the file in a Laravel app
Can'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().
Fix it fast
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/uploads2Match what you see, then jump to its fix
- 1
ls says: No such file or directory for the folder, not the fileThe target folder does not existFix - 2
The folder exists but is owned by another user, for example root after a manual deployThe web server user cannot write to the folderFix - 3
The 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
- 1
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/uploads2Fix
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/uploadsConfirmed by image.intervention.io
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/uploads2Fix
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/uploads3Verify
Upload again and check that the new file is owned by the web server user.
ls -l /var/www/app/public/uploadsConfirmed by raw.githubusercontent.com
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 view3Verify
The file appears on disk and the URL opens in the browser.
ls -l public/uploads/photo.jpgConfirmed by raw.githubusercontent.com
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
Composer says the gd extension is missing on a Laravel project
composer install or composer update stops on a package that requires ext-gd
Composer refuses to install a package that requires ext-gd when the PHP binary running Composer has no GD extension.
4 causes, each with a fixBack 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 fixBack end
Laravel file upload returns 413 behind nginx
413 Content Too Large
A 413 means a limit refused the request body before Laravel could use it.
3 causes, each with a fixBack end
Laravel says the bootstrap/cache directory must be present and writable
The /var/www/app/bootstrap/cache directory must be present and writable.
Laravel writes its package manifest into bootstrap/cache and throws when that folder is missing or the current user cannot write to it.
2 causes, each with a fixBack end
Carbon::now() and now() return the wrong hour in Laravel
created_at values do not match the local time
Carbon::now() uses the PHP default timezone, and Laravel sets that from app.timezone in config/app.php on every boot.
3 causes, each with a fixBack end
Laravel says Class "Redis" not found when using phpredis
Class 'Redis' not found
Laravel uses the phpredis PHP extension by default, so a missing or disabled extension gives Class "Redis" not found.
3 causes, each with a fix