ScottLacey Posted October 30, 2014 Share Posted October 30, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/292153-organizing-file-uploads-into-directories-organized-by-year-month-and-day/ Share on other sites More sharing options...
MDCode Posted October 30, 2014 Share Posted October 30, 2014 (edited) 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 October 30, 2014 by SocialCloud Quote Link to comment https://forums.phpfreaks.com/topic/292153-organizing-file-uploads-into-directories-organized-by-year-month-and-day/#findComment-1495203 Share on other sites More sharing options...
ginerjm Posted October 30, 2014 Share Posted October 30, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/292153-organizing-file-uploads-into-directories-organized-by-year-month-and-day/#findComment-1495207 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.