how to check whether multiple folders exists or not like wordpress style

I am using the following PHP code:

if (!file_exists('../products'))
    {
        mkdir('../products', 0777, true);
        $target = "Main Not Present, ";
    }
else
    {
        if(file_exists('../products'))
            {
                $timezone = new DateTimeZone("Asia/Kolkata");
                $datetime = new DateTime();
                $datetime->setTimezone($timezone);

                $year = $datetime->format('Y');
                $month = $datetime->format('M');
                $day = $datetime->format('D');
                $timestamp = $datetime->format('Y-m-d H:i:s');

                if(!file_exists('../products/'.$year))
                    {
                        mkdir('../products/'.$year, 0777, true);
                        $target = $target."With year not present also, ";
                    }
                else
                    {
                        if(file_exists('../products/'.$year))
                            {
                                if(!file_exists('../products/'.$year."/".$month))
                                    {
                                        $target = $target."With not not present also";
                                    }
                            }
                    }
            }
    }

I am creating an ecommerce website using CMS technique like wp, however i am trying to check whether certain directories exist or not, if theydont then to create them and proceed. The problem with my code is that when this code is encountered true

if (!file_exists('../products'))
{
    mkdir('../products', 0777, true);
    $target = "Main Not Present, ";
}

it creates the directory and the data is stored in the variable, the problem is that it isn’t processign further and going in the else statement whether the other folders in that products folder exists or not.

What I am trying to achieve is that

1.) check the products folder, if it doesn’t exists then create if it exists then proceed futher, lets take worse case that it isn’t present then in this case if i want to create that folder and make my code work further but with this code it only satisfy the 1st if condition and down’t go in the else statement to satisfy the code further.

Can anyone help me with this? Thanks in advance.

Read more here: how to check whether multiple folders exists or not like wordpress style

Leave a Reply

Your email address will not be published. Required fields are marked *