How about a lightweight but smart file storage system? Want to store files without duplicates while still seeing every uploaded filename? And do it all without databases or extra complexity? Easy. To demonstrate this, I built a simple but functional file storage system in PHP CLI. I implemented something similar about 15 years ago in the best CMS, Way To Web 3.0, which I stopped developing seven years ago—but it is still the best. In my subjective opinion.
Back to the storage system. What problems does it solve?
- Files are not duplicated, even if they are uploaded under different names—space savings.
- Thanks to alias.json, you can see all uploaded filenames, so users continue to work with “their” files.
- Files are hashed, so data is not lost or changed—you can verify immutability and detect viruses, malicious code injection, and so on.
- No database is required—just the file system. Fewer technologies means easier setup and management.
- Flexible storage structure—the folders are built from a partitioned SHA-256 hash, so files are distributed automatically and evenly across file-system folders, while folder names remain deterministic. Most importantly, you can build distributed storage: part of the namespace on one server, part on another.
---
What is a file hash, and why is it needed?
A file hash is a unique digital fingerprint calculated from the file content using special cryptographic algorithms such as SHA-256. If even one byte in the file changes, the hash becomes completely different.
What is it good for?
- Determining uniqueness—files with identical content will always have the same hash.
- Protection against changes—if someone alters a file, the hash changes immediately and it becomes obvious.
- Storage optimization—you can store one file under many names, saving space.
- Fast integrity checks—you can easily verify whether a file was corrupted during transmission.
---
Where is this useful?
- When you need a simple S3-like storage layer.
- When users upload files, but you do not want to store duplicates.
- When you want minimal server overhead and simple code.
---
How does it work?
1. A file is uploaded into storage through store.php.
2. The SHA-256 hash of the file is calculated.
3. A folder path is formed by taking the first characters of the hash and building nested directories.
4. If the file already exists, its name is simply recorded in alias.json.
5. The file is always stored in a single copy, even if it is uploaded 100 times under different names.
You now have a simple, reliable, database-free storage system with smart file handling. Minimal code, maximum value. Now you can build your own S3 on PHP CLI!