1

I have the code below. I want to create folders if they are not already created. Can I do it more elegant way? More dynamic? Can I call mkdir() this way?

$array = array('Stylesheets' => 'css', 'Javascript' => 'js') //ETC
    mkdir($array, 0777, true);

Thanks.

<?php
header('Content-Type: text/html; charset=utf-8'); 
?>

Creating directories:

<?php

if ( !file_exists('js') ) {
    mkdir('js', 0777, true) ;
    echo 'Js directory has been created.';
}

else
    echo 'Js directory already exists.';

if ( !file_exists('css') ) {
    mkdir('css', 0777, true) ;
    echo 'Css directory has been created.';
}

else
    echo 'Css directory already exists.';

if ( !file_exists('img') ) {
    mkdir('img', 0777, true) ;
    echo 'Img directory has been created.';
}

else
    echo 'Img directory already exists.';

?>

1 Answer 1

4

Loop through the array to create directories dynamically, like this:

$array = array('Stylesheets' => 'css', 'Javascript' => 'js');

foreach($array as $dir){
    if ( !file_exists($dir) ) {
        mkdir($dir, 0777, true) ;
        echo $dir . ' directory has been created.';
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.