1
Inspired by another post in this sub, I made a PHP function for sorting files into folders for year, month, day
(zerobytes.monster)
The original post: /r/datahoarder by /u/JamesRitchey on 2025-03-24 23:18:13.
Inspired by this other post, I made a PHP function for copying files, and sorting them into folders by date factors.
Download Link: https://github.com/jamesdanielmarrsritchey/ritchey_copy_and_sort_files_i1
Pros:
- Open source
- Copies files, but doesn't do anything with the originals.
- Can create sub-folders for year, month, and/or day (e.g. '/year/month/day' '/year/month' '/day'), provided the mixture doesn't result in file collisions.
Cons:
- This is just something I whipped up, so it has had limited testing. Use at own risk. The largest test I did was with 2,862 files.
- It relies on an array to store a list of all the files it needs to process, and for its return.
- Designed with Linux paths in mind. Compatibility with Windows untested, and unknown.
Other Considerations:
- Uses date modified.
- Fails on file collision, rather than renaming files.
Example Script:
<?php
$location = realpath(dirname(__FILE__));
require_once $location . '/ritchey_copy_and_sort_files_i1_v1.php';
$return = ritchey_copy_and_sort_files_i1_v1("{$location}/temporary/Original", "{$location}/temporary/Copy", TRUE, TRUE, TRUE, NULL);
if (@is_array($return) === TRUE){
print_r($return);
} else {
echo "FALSE" . PHP_EOL;
}
?>
Example Return:
Array
(
[0] => Array
(
[source_file] => /home/user1/Public/ritchey_copy_and_sort_files_i1_v1/temporary/Original/Example 2.txt
[destination_file] => /home/user1/Public/ritchey_copy_and_sort_files_i1_v1/temporary/Copy/2022/September/21/Example 2.txt
)
[1] => Array
(
[source_file] => /home/user1/Public/ritchey_copy_and_sort_files_i1_v1/temporary/Original/Example 1.txt
[destination_file] => /home/user1/Public/ritchey_copy_and_sort_files_i1_v1/temporary/Copy/2020/March/24/Example 1.txt
)
[2] => Array
(
[source_file] => /home/user1/Public/ritchey_copy_and_sort_files_i1_v1/temporary/Original/Sub Folder/Example 3.txt
[destination_file] => /home/user1/Public/ritchey_copy_and_sort_files_i1_v1/temporary/Copy/2024/January/3/Example 3.txt
)
)