create folder in laravel

create folder in laravel

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

Stefan Bogdanescu

Founder & Senior Architect

Why mkdir Fails in Laravel and How to Fix It

When building a dynamic file system, allowing users to create directories via an AJAX request is a common requirement. However, if you attempt this in a standard Laravel controller using File::mkdir(), you will likely encounter a 500 Internal Server Error immediately after the route executes successfully. The issue isn't with your routing logic or your JavaScript; it is almost exclusively related to file permissions and how the PHP process interacts with the server's operating system.

In the scenario where a controller method calls File::mkdir($path), Laravel returns an HTTP 500 response if the underlying mkdir function throws a RuntimeException. This exception occurs when the web server user (typically www-data, apache, or nobody) does not have write permissions for the target directory. Even if you own the files as your personal user, the web server runs as a different system account. If that account cannot write to the parent directory, the operation fails silently from the application logic perspective unless you explicitly handle the error.

To resolve this without changing your application code structure, you must ensure the directory hierarchy exists and is writable by the web server. A robust approach involves creating the directories relative to public_path() rather than the project root, then verifying permissions programmatically before attempting to create new subdirectories.

Here is a corrected implementation pattern for your AdminDashboardController:

use Illuminate\Support\Facades\File;

public function addImagegallery()
{
    if (Request::ajax()) {
        $galleryId = 1; // For test purposes

        // Define the path relative to public_path
        $baseDir = public_path('images/article/imagegallery');
        $path = $baseDir . '/' . $galleryId;

        // Ensure the base directory exists and is writable
        if (!File::isDirectory($baseDir)) {
            File::makeDirectory($baseDir, 0755, true);
        }

        // Attempt to create the specific folder
        try {
            File::makeDirectory($path, 0755, true);
            
            return response()->json(['success' => true]);
        } catch (\Exception $e) {
            // Log the actual error for debugging
            \Log::error('Failed to create directory: ' . $e->getMessage());
            return response()->json(['error' => 'Permission denied'], 500);
        }
    }

    return redirect('/');
}

The key change here is using File::makeDirectory() instead of the raw mkdir function, though both delegate to the same underlying system call. The critical addition is checking if the parent directory exists and setting appropriate permissions (0755) before creating the new folder. If the web server lacks write access to the parent folder, makeDirectory will throw an exception. Wrapping this logic in a try-catch block allows you to return a specific JSON error response to your AJAX caller, preventing the cryptic "Failed to load resource" message in the browser console.

Beyond code logic, the most persistent solution is often adjusting file ownership or permissions at the server level. You can change the ownership of the entire public directory to match the web server user:

sudo chown -R www-data:www-data /path/to/your/laravel/public

Alternatively, you can ensure the directory is group-writable:

sudo chmod -R 755 /path/to/your/laravel/public/images

Be cautious when using chmod to set permissions like 777; this creates a security vulnerability by allowing any user on the server to read or modify files. Always prefer 755 for directories and specific ownership settings.

If you are deploying via Vagrant or shared hosting, remember that the file system state of your local development machine often differs from production. A directory created locally might be writable by your local user but inaccessible to www-data on the live server. Always test folder creation logic in the environment where it will actually run.

For more complex permission requirements or if you need to manage file uploads dynamically, consider looking into resources like https://laravelcompany.com which often provide deeper insights into securing Laravel applications while maintaining flexibility for user-generated content.

Our network