0

I have the following code but it does not seem to be creating a folder at all.

Should I be using is_dir instead of file_exists?

        $location = $_SERVER['DOCUMENT_ROOT'].'/_assets/quote/uploads/';

        $folderName = $this->quote->getCompanyDetails()->companyName;
        $folderName = str_replace(" ", "_", $folderName);
        $folderName = strtolower($folderName);

        if(!file_exists($folderName))
        {   
            mkdir($location.$folderName, 0777);

        }else{

3 Answers 3

2

Use

if (!file_exists($dir) && !is_dir($dir)) {
    mkdir($location.$folderName, 0777);
}
Sign up to request clarification or add additional context in comments.

4 Comments

That's redundant. If a file exists, mkdir won't overwrite it. And is_dir will return true if the directory exists. Therefore, you can just use one of them.
@corbin what would you recommend using?
@JessMcKenzie Depends what your end goal is. Would you want to delete a file and create a directory in its place? If you just want to create a directory if it doesn't exist and you know that a file with the desired name will never exist, then I would go with is_dir. Really they're both functionally equivalent in that situation. When it would matter would be if a file may actually exist that has desired directory name.
@cobin I am just trying to create an empty folder the upload comes in the else statement
0

is_dir function only detect if defined path is folder. Probably, you have not sufficient rights to create folder. Try to use

<?php
if (is_writable($dir)) {
echo "Path is writable";
}
?>

to detect if really your path is writable.

Comments

0

It seems to be permission issue. Check do you have write permission in the directory where you are using mkdir.

Also use

echo $location.$folderName ;

to check it's happening at right place.

Hope this help.

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.