Reputation: 63
How to create folder in php.I tried this code.It's not creating folder in parent directory.
$dir1="http://www.consul.com/hrd/b";
mkdir($dir1, 0755);
Upvotes: 1
Views: 1420
Reputation: 3832
You've set $dir1 to a URL instead of providing a path. Also, if any of the directories don't exist, you may need to use the recursive paramater, as follows:
<?php
$dir1 = "/hrd/b";
if (!mkdir($dir1, 0755, true)) {
die('Failed to create folders...');
}
For more info about this function, you may wish to read http://php.net/mkdir.
I tried this code on a Windows Box using PHP5.4.3, included in WampServer 2.2 (uses Apache 2.2.22) and it worked perfectly.
When I tried the code on a Linux Server running a very early version of PHP5, I had to tweak the path and this is the code that finally worked there:
<?php
$dir1 = dirname(__FILE__)."/hrd/b";
if (!mkdir($dir1, 0755, true)) {
die('Failed to create folders...');
}
Note: dirname() and its parameter, a magic constant, together provide the full path of the currently executing PHP script and that info helps in turn to provide the full path info for $dir1.
Upvotes: 0
Reputation: 617
You can use this code to create a directory in the parent dir.
if (!file_exists("../dir_name/")) { mkdir("../dir_name/", 0777, true); }
Upvotes: 0
Reputation: 12039
Use dirname(__FILE__)
to get root directory.
To get root directory
For PHP >= 5.3.0 try
__DIR__
For PHP < 5.3.0 try
dirname(__FILE__)
$root = dirname(__FILE__);
$dir1 = $root . "/your_folder";
mkdir($dir1, 0755);
EDIT : Also can create directory by checking it consistency if required.
$root = dirname(__FILE__);
$dir1 = $root . "/your_folder";
if(!is_dir($dir1)){
mkdir($dir1, 0755);
}
Reference:
Upvotes: 1
Reputation: 427
mkdir()
uses the local directory path, not the server directory path. See this link for more info.
You'd want to run the following code:
$dir = __DIR__ . "hrd/b"; // This gets the server's root directory and creates "hrd/b" relative to it.
mkdir($dir, 0755);
Note that for PHP versions < 5.3.0, you'll need to use dirname(__FILE__)
instead of __DIR__
.
Upvotes: 1
Reputation: 5356
try this
$dir1="/home/UserName/public_html/DirName";
mkdir($dir1, 0755);
Upvotes: 0