views:

189

answers:

1

Hi, Want to create a directory on windows from a PHP script.

My script is in www/Test directory of Apache and want to create a folder(fold1) inside www/downloads directory.

Inside the script, using,

$dirName = "../downloads/fold1";
mkdir("{$dirName}");

If we use the full path of dirName like C:\Apache\www\downloads\fold1, it works fine.

But want to use relative path since this code will be sent to the client.

Please let me know. Thanks, Vish.

A: 

I would guess your current directory is different from your files folder, so you have to use a trick:

mkdir(dirname(__FILE__) . "/" . $relative_path);

dirname(__FILE___) returns the absolute path of your current php file. With this you can build an absolut path.

ZeissS
With php 5.3 you can even use `__DIR__` instead of `dirname(__FILE__)`
Arkh
this absolute path is no better than relative one.
Col. Shrapnel
and guessing is not the thing a programmer should ever rely on.
Col. Shrapnel
Why is it not better?
ZeissS
because it's still relative from the current script location.
Col. Shrapnel
No? By prefixing it with the absolute path of the script you get an absolute path. Yes it contains relative elements by using .. and so on, but I don't see any problems with that when his app gets deployed to a customer.
ZeissS