Search code examples
phpmkdir

mkdir problem PHP


Warning: mkdir() [function.mkdir]: No such file or directory in

I keep getting this annoying message when trying to create new directories. my function is

mkdir("../".$a."/".$b);

$a = an existing filepath
$b = new folder i wish to create

function is executed from another directory: my structure looks like this:

/htroot/site/c/ <- where im executing the function
/htroot/site/a/b <- where i wish to create the directories.

if i execute the following, it creates the desired effect but in the same directory as the function.

mkdir($a."/".$b);

HI ALL THANKS FOR THE HOT RESPONSES

C:\wamp\www\book\admin\import //is my __DIR__ for that script
C:\wamp\www\book\admin\property // already exists

C:\wamp\www\book\admin\property\name // want i want end result

Solution

  • Depending on how your function is included, the PWD could be anywhere. You're best to use an absolute path.

    You can also grab the current script's directory using __DIR__ (v5.3+) or dirname(__FILE__)

    For example

    // use realpath to resolve any symbolic links
    $newDir = realpath(__DIR__ . '/../' . $a) . '/' . $b;
    mkdir($newDir);
    

    See realpath()