Jump to content

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


ScottLacey

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:

 

attachments/

--/2014

-----/January

--------/01

--------/02

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

-----/February

--------/01

--------/04

--------/09

--------/18

--------/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.

 

 

Link to comment
Share on other sites

Untested

<?php
 
// Base path starts with attachments
$upload_path = "attachments/";
 
// Check if the year directory exists, if not, create it
if(is_dir($upload_path.date("Y")))
    $upload_path .= date("Y")."/";
else {
    mkdir($upload_path.date("Y"));
    $upload_path .= date("Y")."/";
}
 
// Next, check the month by word
if(is_dir($upload_path.date("F"))
    $upload_path .= date("F")."/";
else {
    mkdir($upload_path.date("F"));
    $upload_path .= date("F")."/";
}
 
// Next check the day of the month with leading zeros
if(is_dir($upload_path.date("d"))
    $upload_path .= date("d")."/";
else {
    mkdir($upload_path.date("d"));
    $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
Link to comment
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

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

Guest
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.

×
×
  • 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.