Topic: Put Images in Folders Year,month,day

I saw some one ask this so thought I would look at it.

//this is my code to have images dumpped into folders based on year/month/day like 2010/4/13/image.png

$xyear = date('Y');
$xmonth = date('M');
$xday=date('d');
$xpath = "images/".$xyear."/".$xmonth."/".$xday."/";
define('DIR_IM',$xpath);

mkdir("images/".$xyear, 0700);
mkdir("images/".$xyear."/".$xmonth, 0700);

mkdir($xpath, 0700);

Open Config.php

Find "define('DIR_IM'"

And replace it with the code above, what it will do is create, or try and create the folder's for the current year, month and day of the month, it will then build that into the current DIR_IM as a path.

Any new uploads will then be put in those locations.

like www.imagehost.com/images/2010/Mar/25/myimage.png

Thumbs up

Re: Put Images in Folders Year,month,day

Warning: mkdir() [function.mkdir]: File exists in /var/www/xxx/config.php on line 43

Warning: mkdir() [function.mkdir]: File exists in /var/www/xxx/config.php on line 44

Warning: mkdir() [function.mkdir]: File exists in /var/www/xxx/config.php on line 46

i have this problem  sad

Thumbs up

Re: Put Images in Folders Year,month,day

the dragon wrote:

Warning: mkdir() [function.mkdir]: File exists in /var/www/xxx/config.php on line 43

Warning: mkdir() [function.mkdir]: File exists in /var/www/xxx/config.php on line 44

Warning: mkdir() [function.mkdir]: File exists in /var/www/xxx/config.php on line 46

i have this problem  sad

The folder's already exist, and your webserver is just telling you that, not sure how to ignore those warnings, happily mine does not warn like this. if you find a solution let me know.

Thumbs up

Re: Put Images in Folders Year,month,day

Yavin wrote:
the dragon wrote:

Warning: mkdir() [function.mkdir]: File exists in /var/www/xxx/config.php on line 43

Warning: mkdir() [function.mkdir]: File exists in /var/www/xxx/config.php on line 44

Warning: mkdir() [function.mkdir]: File exists in /var/www/xxx/config.php on line 46

i have this problem  sad

The folder's already exist, and your webserver is just telling you that, not sure how to ignore those warnings, happily mine does not warn like this. if you find a solution let me know.

I too am having this problem. Does anyone know a solution to this? If this can be resolved then this has been the mod I've been looking for!

Thumbs up

Re: Put Images in Folders Year,month,day

You should write some "IF" that seeks if the folder already exist, if not then creates the folder.

rodolfoberrios.com | PLEASE use Tech Support forums to ask for support!

Re: Put Images in Folders Year,month,day

if(is_dir("images/".$xyear)){
    //do something
}else{
    mkdir("images/".$xyear, 0755);
}


if(is_dir("images/".$xyear."/".$xmonth.$xyear)){
    //do something
}else{
    mkdir("images/".$xyear."/".$xmonth, 0755);
}

if(is_dir($xpath)){
    //do something
}else{
    mkdir($xpath, 0755);
}

Its not neat but it should work. just checks the see if the path is a DIR, if it is TRUE, then it does nothing, if it is FALSE then it makes the dir.

code is untested so let me know if you have issues.

Thumbs up

Re: Put Images in Folders Year,month,day

Found a problem with this, dealing the the viewer code.

host.com/?v=myimage

the default code looks for the image name inside the images folder, and not inside these new folders that are created each day, I thought a solution would be to simply supply the full path to v so like ?v=2010/May/01/myimage.png but I cant seem to get this working.

Anyway else know what bit of code I need to be looking at ? or have a fix for it. ?

Also the above date , day and year code also needs to be applied to your thumbnail DIR_TM so that all thumbnails are put in the correct folders as well. or you will have TM issues.

Thumbs up

Re: Put Images in Folders Year,month,day

the "?v" works by default in just one folder. In order to make this work you may (1) use db to store the folder information for each image or (2) provide the right "?v" after upload the file, this means that you have to change all the "mechanics" in "?v"

rodolfoberrios.com | PLEASE use Tech Support forums to ask for support!

Re: Put Images in Folders Year,month,day

Rodolfo wrote:

the "?v" works by default in just one folder. In order to make this work you may (1) use db to store the folder information for each image or (2) provide the right "?v" after upload the file, this means that you have to change all the "mechanics" in "?v"

Yeah I was thinking ?v=2010/May/01/myimage.png

but while the code is english most of the variables and stuff is spanish or some other language so its kinda hard some times to track whats going on, you dont happen to know what files the code pertaining to the v paramater are ? in terms of viewing and displaying of the image passed to it ?

Thumbs up

Re: Put Images in Folders Year,month,day

Is not too hard...

After this:

if ($modo==2 || $modo==3) {
    // INFORMACION (ANCHO, ALTO y PESO)
    if ($modo==2) {
        if ($_GET['v']) {
            $id = $_GET['v'];

you will find this:

$imagen = DIR_IM.$id;

So if id is "2010/May/01/myimage.png" the $imagen needs to fit in that... and you need to re-make the displays because "id" is for image files not directory+images in the original script.
got it?

rodolfoberrios.com | PLEASE use Tech Support forums to ask for support!

Re: Put Images in Folders Year,month,day

Now I know nothing more  sad  roll

Thumbs up

Re: Put Images in Folders Year,month,day

I just can't get this, my images donesn't show at all .. sad

$imagen = DIR_IM.$id;

- don't understnd this part... sad

Thumbs up

Re: Put Images in Folders Year,month,day

use this code

$xyear = date('Y');
$xmonth = date('M');
$xday=date('d');
$xpath = "images/".$xyear."/".$xmonth."/".$xday."/";
if(!is_dir($xpath)){
mkdir("images/".$xyear, 0777);
mkdir("images/".$xyear."/".$xmonth, 0777);
mkdir("images/".$xyear."/".$xday, 0777);
}
define('DIR_IM',$xpath);

Thumbs up

Re: Put Images in Folders Year,month,day

small update to dedydamy code

$xyear = date('Y');
$xmonth = date('M');
$xday = date('d');
$xpath = "images/".$xyear."/".$xmonth."/".$xday."/";
if(!is_dir($xpath)){
    if(!is_dir("images/".$xyear)){
        mkdir("images/".$xyear, 0777);
    }
    if(!is_dir("images/".$xyear."/".$xmonth)){
        mkdir("images/".$xyear."/".$xmonth, 0777);
    }
    
    if(!is_dir("images/".$xyear."/".$xmonth."/".$xday)){
        mkdir("images/".$xyear."/".$xmonth."/".$xday, 0777);
    }
}

define('DIR_IM',$xpath);

There is another problem though. How can we use this new code while we have other old images in "/images" subfolder ? Maybe we can implement a check, if not found then search in /image ?
Any ideas about that ?

Last edited by olejka2k (2010-11-07 19:28:52)

Thumbs up

Re: Put Images in Folders Year,month,day

olejka2k wrote:

.....
$xmonth = date('M');
........

I changing this for :
$xmonth = date('m_M');
good sorting in folder


and, i put thumbs in Y-M-D folder too, i think its be convenient.
for this find

define('DIR_TH','thumbs/');


and replace

//Year-mounth-day for Thumbs
$xpath_thumbs = "thumbs/".$xyear."/".$xmonth."/".$xday."/";
if(!is_dir($xpath_thumbs)){
    if(!is_dir("thumbs/".$xyear)){
        mkdir("thumbs/".$xyear, 0777);
    }
    if(!is_dir("thumbs/".$xyear."/".$xmonth)){
        mkdir("thumbs/".$xyear."/".$xmonth, 0777);
    }
    
    if(!is_dir("thumbs/".$xyear."/".$xmonth."/".$xday)){
        mkdir("thumbs/".$xyear."/".$xmonth."/".$xday, 0777);
    }
}

define('DIR_TH',$xpath_thumbs);
//

Last edited by Vermin (2010-11-08 14:25:28)

Thumbs up