Laravel 5.3 Storage::put creates a directory with the file name

Laravel 5.3 Storage::put creates a directory with the file name

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

Stefan Bogdanescu

Founder & Senior Architect

Laravel 5.3 Storage::put creates a directory with the file name

If you are running into an issue where Storage::put in Laravel 5.3 seems to be creating a subdirectory named after your file before placing the content inside it, you are likely misunderstanding how the put method interprets its arguments. This behavior is not a bug in the framework; rather, it is a direct consequence of how Laravel's filesystem abstraction handles path resolution and directory creation.

When you call Storage::disk('local')->put($newFilename, $file), the first argument is treated as a relative file path. If your $newFilename variable contains a dot (.), such as in your example where you construct $hashedName . $timestamp . '.' . $file->getClientOriginalExtension(), the filesystem driver interprets everything before the final dot as a directory path.

In your specific code snippet, if $newFilename resolves to something like 952d6c009.jpg/952d6c009.jpg, the logic is actually simpler but often misunderstood: the first argument defines the full destination path. If that string contains a forward slash (/) or backslash (\) before the final filename component, Laravel assumes you are targeting a subdirectory.

However, looking closely at your variable construction:

$newFilename = $hashedName . $timestamp . '.' . $file->getClientOriginalExtension();

This results in a string like 952d6c009.jpg1700000000000.jpg. There is no directory separator here. So why is Laravel creating a directory?

The confusion often stems from how the $file object is passed to put. In your code, you pass the entire file object ($file) as the second argument. While Storage::put accepts a string of content for the second argument, passing an instance of Illuminate\Http\UploadedFile (or similar) can behave differently depending on the underlying driver implementation in specific versions.

The most critical issue here is that you are not actually using the $file object's contents correctly with Storage::put. The put method expects a string for the second parameter when storing raw content. When you pass the file object directly, some drivers might attempt to handle it as a resource or might be misinterpreting the path if the path string itself is malformed.

But there is a more common pitfall: Path Resolution. If your $newFilename variable inadvertently contains a directory separator, Laravel will create that directory. For example, if you accidentally added a slash between the hash and the timestamp, or if the extension logic introduced one, the driver creates the intermediate folder.

To fix this and store the file flat in storage/app/public (or wherever your disk root is), ensure your path string contains no slashes until the very end. Furthermore, you should extract the actual contents of the uploaded file before passing it to put. The put method signature for storing files typically looks like this:

Storage::disk('local')->put($path, $content);

If you want to store an uploaded file without creating unnecessary subdirectories, convert the file object to a stream or a string first. You can use the getRealPath() method or stream it directly:

// Extract the real path of the uploaded file
$stream = $file->getRealPath();

// Put the content into storage using the simple filename
Storage::disk('local')->put($newFilename, $stream);

Alternatively, if you are using Laravel 5.3 and want to leverage the move functionality which is often safer for handling uploads, consider moving the file directly from the temporary upload directory to your destination:

// Move the file from the temp location to the final path
Storage::disk('local')->put($newFilename, $file->getContent());

Wait, strictly speaking in 5.3, put with a file object isn't standard practice for moving files; usually, one would use move. If you are strictly using put, ensure the second argument is a string representing the binary data. Using $file->getRealPath() returns a string path to the temporary file on disk. However, passing that path to put might try to read and write it again.

A cleaner approach in 5.3 involves reading the stream:

$stream = fopen($file->getRealPath(), 'r');
Storage::disk('local')->put($newFilename, $stream);
fclose($stream);

If you simply want to avoid the complexity of put and the directory creation confusion entirely, use Laravel's built-in move method which handles the transfer from the upload temp folder to your custom path in one go:

$destination = storage_path('app/public/' . $newFilename);
$request->file($fileInForm)->move(
    public_path(), // Or your specific target directory
    basename($newFilename) // Ensure only the filename, not the path
);

The core lesson here is that Storage::put creates directories whenever the first argument includes a path separator. If your generated $newFilename somehow resolves to path/to/filename.ext, Laravel dutifully creates path/to/. Always sanitize and validate your filename string construction to ensure no accidental slashes are introduced before the final file extension.

Our network