mkdir(): Permission denied Laravel
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Resolving mkdir(): Permission Denied Errors in Laravel Image Uploads
Introduction
Laravel is an amazing PHP framework that provides several built-in functions to perform different tasks. The `mkdir()` function creates directories on the server, but sometimes it throws errors like "Permission denied" when running on a production server. In this comprehensive post, we will discuss various solutions and best practices for handling these issues in Laravel image uploads.
The Code Example
Let's first look at the code you provided:
```php
$user_id = Auth::id();
$logicpath = 'userdp/' . $user_id . '/';
$pubpath = 'userdp/' . $user_id . '/' . $dpFile;
$path = '/userdp/' . $user_id . '/' . $dpFile;
if (!file_exists($logicpath)) {
mkdir($logicpath, 0777, true);
}
```
The Issues and Potential Solutions
1. Missing chmod 777 on public:
Command `chmod 777 public` is not the right way to set permissions. The "public" folder often contains public files like CSS or Javascript files, which should ideally have different permission levels than your application folders. You may want to consider changing the path where you're trying to create the new directory (userdp).
2. Incorrect Paths and Directory Structure:
Sometimes, the paths provided in the code can result in errors due to incorrect file permissions on the server. Ensure that the directories exist before running mkdir() and use appropriate permission levels for each folder.
3. Missing Necessary Permissions:
In Laravel, when you run a command like `php artisan serve`, it starts your app on localhost using PHP's built-in web server. This type of server runs as the same user that started the script, usually www-data or the apache user. When deploying to production servers, these users might not have write permissions to certain folders.
Solutions:
1. Check and Set Correct Permissions on Folders:
Ensure you've set the correct file permissions on your Laravel project folders, including www-data and the userdp folder, allowing them to read/write access. You may need to adjust these settings through SSH or the server control panel.
2. Use Laravel Migration Commands:
Instead of using mkdir() in your code, try running a migration command like `php artisan migrate` to create a new table and directory structure for your application. This ensures that Laravel handles any necessary file permissions on its own.
3. Check Access Rights with File_GetFileInfo():
Use the `file_get_contents($path)` function to get the existing file permissions for the desired folder, check if it allows writing access, and modify your mkdir() command only if required:
```php
$permissions = file_get_contents('userdp/'.$user_id.'/'); // This is a path to your user directory
if($permissions !== false && !is_writable($path)){
// Set new permissions to 0755 or whatever suits your project requirements
$mkdirResult = mkdir($logicpath, 0777, true);
}
```
4. Use Laravel's File System Manager:
If possible, delegate file operations to Laravel's built-in File System Manager using `Storage::makeDirectory()`. This can help you avoid permission issues because Laravel will handle the permissions for you:
```php
$user_id = Auth::id();
$path = Storage::makeDirectory('public/userdp/' . $user_id); // Create / Userdp folder and subfolder with correct permissions
```
Conclusion
In summary, by making sure your Laravel project folders have the necessary permissions, utilizing Laravel's built-in methods to handle file operations, and checking access rights before attempting to modify them in code, you can easily tackle permission issues like "mkdir(): Permission denied" errors. With these best practices in place, your Laravel image upload system will be more secure and reliable for both local development and production environments.