View Full Version : Create date folders with mkdir().

01-20-2008, 10:02 AM
Trying a little test and I'm not sure this is correct.

Using date("d/m/Y"); will give you the current date as 01/01/2008.
So to try and create folders based on the date I tried this code here:

$date = date("d/m/Y");
list($day, $month, $year) = split('[/.-]', $date);
echo " Day: $day; Month: $month; Year: $year<br />\n";
echo $date;

mkdir("/uploads/$year/$month/$day", 0700);

The mkdir function was not working. What I did find was that if I just tried the $year with the $month/$day all was well. So using this code here:

mkdir("/uploads/$year/", 0700);
mkdir("/uploads/$year/$month/", 0700);
mkdir("/uploads/$year/$month/$day", 0700);
I was able to create folders based on todays date. Is it possible to create the folder in one line rather that what I got to work?

01-20-2008, 10:38 AM
what i do is build my linux command up

run it from a command line and see if it does what i want then copy the command to PHP

mkdir -p 2007/19/08

would make the following structure

2007 > 19 > 08

so in PHP i do this...

$command = "mkdir -p $year/$month/$day";



you may have to chmod the parent dir to whatever your requirements are using php chmod() or a system chmod as above