Jump to content
Sign in to follow this  

Organizing File Uploads Into Directories Organized by Year, Month and Day

Recommended Posts

I have a file upload script that will eventually process a ton of files.  I would like to upload them into sub-directories according to what year, month, and day they are uploaded.  


A typical tree should look like this:







--------/03 , etc.






--------/20, etc

-----/March, etc

--/2015, etc.


So a file called image.jpg uploaded on 10/31/2014 would have a URL of attachments/2014/October/31/image.jpg.  I understand that every time a file is uploaded, the script would have to detect through FTP whether or not folders for the year, month, and day exist, and if they don't create them.  My problem is that I have no idea what the logic of this script would be.  What order should I do things in?  Is there a way to use maybe foreach to detect/create the folders?  Any input would be appreciated.



Share this post

Link to post
Share on other sites


// Base path starts with attachments
$upload_path = "attachments/";
// Check if the year directory exists, if not, create it
    $upload_path .= date("Y")."/";
else {
    $upload_path .= date("Y")."/";
// Next, check the month by word
    $upload_path .= date("F")."/";
else {
    $upload_path .= date("F")."/";
// Next check the day of the month with leading zeros
    $upload_path .= date("d")."/";
else {
    $upload_path .= date("d")."/";
echo $upload_path;

That should get your upload path created if it doesn't exist, or use it if it does.  The path should be accessible through the $upload_path variable.

Edited by SocialCloud

Share this post

Link to post
Share on other sites

What's the point of storing uploads into separate dirs.?  Are people going to browse these folders?  Cause if they are not, then why go to such lengths to organize them into such a fine level of detail?  I have to believe that you are also storing attributes of these files (one of them being the upload date)  in a db somewhere and you could simply store the location of the file and its name as a pair of these attributes and use that to locate the file when called for.

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.