Laravel uploading file Unable to write in directory

Laravel uploading file Unable to write in directory

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Laravel File Upload Failure on Ubuntu: Beyond Basic Permissions

When a file upload works flawlessly in a local XAMPP environment but fails immediately upon deployment to an Ubuntu server, the issue almost invariably shifts from application logic to operating system permissions. The specific error "Unable to write in directory" indicates that the web server process lacks the necessary privileges to touch files within your target path.

In your scenario, you have correctly identified that the public/system directory requires writable permissions. You attempted to resolve this using chmod 770, which grants read, write, and execute access to the owner and group but restricts others. While logically sound for Linux security models, this approach often fails on production servers like DigitalOcean because the web server runs as a specific user—typically www-data or apache—which might not be the file owner or part of the intended group.

The Core Issue: Ownership vs. Permissions

The fundamental misunderstanding in many deployment scenarios is equating permissions with ownership. Setting chmod 770 on a directory owned by your personal Linux user (e.g., john) will not help if Apache runs as www-data. The web server process cannot write to files owned by another user unless that user has explicit write access, or the file is owned by the web server itself.

On Ubuntu-based systems like DigitalOcean, the standard web server runs under the www-data user. Therefore, the most robust solution is not just changing permissions, but ensuring the target directory is owned by www-data. You can achieve this with a combination of ownership and permission commands:

sudo chown -R www-data:www-data /var/www/html/public/system
sudo chmod -R 755 /var/www/html/public/system

This configuration ensures that the owner (www-data) has full control (rwx), the group has read and execute access, and others have read-only access. This follows the principle of least privilege while guaranteeing functionality for the web server process.

Laravel-Specific Configuration

Even with correct filesystem permissions, Laravel 5 and later versions introduce an additional layer of security via configuration. By default, Laravel attempts to protect sensitive files by preventing symbolic links to directories outside the public folder or ensuring that writable paths are strictly controlled.

However, a critical setting often overlooked is the filesystems configuration within config/filesystems.php. If your application relies on the default disk (local) for uploads, ensure that the root path points exactly where you intend. More importantly, check if your .env file has restrictive settings or if you are inadvertently trying to write outside the public directory without proper flags.

In your code snippet:

$destinationPath = public_path().'/system';
$file = Request::file('add_students')->move($destinationPath, $fileName);

The move operation attempts to relocate the uploaded file from the temporary upload directory (usually /tmp) to $destinationPath. If the destination path is not writable by the web server user, the MoveFailedException will be thrown.

Handling Permission Errors Gracefully

While fixing permissions is the primary solution, it is worth noting that simply running commands via PuTTY might not persist if your deployment process involves a VPS restart or a container orchestration layer like Docker. If you are hosting on DigitalOcean using Droplets, ensure you run these commands as root or with sudo.

If you prefer a more dynamic approach without changing system ownership, you can configure the directory to be world-writable (e.g., chmod 777), but this is generally discouraged in production environments due to security risks. A safer middle ground often involves creating a dedicated group, adding www-data to that group, and setting the directory permissions to 2770 (SGID bit set) or simply ensuring ownership matches the web server user as demonstrated above.

For more complex file management needs, such as handling large datasets from Excel files, consider implementing a cleanup job using Laravel's scheduler to clear out old temporary uploads before they fill up disk space or cause permission race conditions. Always validate that the directory exists and is writable before attempting the upload logic in your controller to provide clearer error messages to developers.

For further guidance on managing file storage and configuration in production environments, resources such as https://laravelcompany.com can offer additional insights into securing your application's file structure.

Our network