-4

I have to create a directory in apache server but it looks like if doesn't recognize the coomand. this is my code.

<?php



mkdir("var/www/devData/",0777);
print "created";

?>

when i launch the file.php i have as output "created" but after i use the shell for check if the dir is present but there is no dir. Anyone can help me?

Edivad
  • 31
  • 1
  • 5
  • 3
    [Enable error reporting](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php) and check `mkdir()`'s return value, which is a boolean indicating success. – CodeCaster Jul 30 '12 at 13:31
  • Whatever the error is, it's almost assuredly been asked before http://stackoverflow.com/search?q=%5Bphp%5D+mkdir – Mike B Jul 30 '12 at 13:39

5 Answers5

2
error_reporting(E_ALL);
ini_set('display_errors', 1);

// a new directory in your current working directory (cwd)
$path = dirname(__FILE__) . "/your/path";

if(!mkdir($path, 0777, true)) {
    echo "Failure";
} else {
    echo "Success";
}

I assume you do not have the permissions to create that path. You may check if you are able to write to that directory with is_writable() The mkdir() documentation tells you more.

fdomig
  • 4,417
  • 3
  • 26
  • 39
  • as i was suppose the path is not correct but how can i know the correc path? i'm working in different path of where i want create the folder how can set that path? – Edivad Jul 30 '12 at 13:41
  • I edited the answer. You just set the `$path` variable according to the path where you want to create your directory. – fdomig Jul 30 '12 at 13:45
  • the path is not good how can i check the correct path? because i have to create that folder in apache but in another dir that is different of the dir where i'm actually work – Edivad Jul 30 '12 at 13:50
  • The path that **you** want to use has to be set by **you**. I am sorry but I have no idea where you want to create a directory. Try using `echo getcwd();` to print the directory where you are working right now. See http://at.php.net/manual/en/function.getcwd.php – fdomig Jul 30 '12 at 13:53
  • i'm working in that directory XXX/YYY and i want create the folder in that directory CCC/VVV/ – Edivad Jul 30 '12 at 13:56
  • You should read how (linux/unix) directories are structured. e.g. http://www.tuxfiles.org/linuxhelp/linuxdir.html/ – fdomig Jul 30 '12 at 13:58
  • the path si that /var/www/xxx/yyy/xx and i want to create the folder there var/www/devData/ how can i do? – Edivad Jul 30 '12 at 13:59
  • add a `/` in front of your path and read the links I provided! – fdomig Jul 30 '12 at 14:00
  • I have that result but i put another path why is still take that path? and i can't see the new dir Warning: mkdir(): File exists in /var/www/dev/affect/NewDemo/JS/dirr.php on line 6 Failure – Edivad Jul 30 '12 at 14:07
  • What could `Warning: mkdir(): File exists` probably mean?!? Seriously, I can't help you sir. – fdomig Jul 30 '12 at 14:10
  • error_reporting(E_ALL); ini_set('display_errors', 1); if(!mkdir("/var/www/devData/", 0777)) { echo "Failure"; } else { echo "Success"; } – Edivad Jul 30 '12 at 14:11
  • Hey man thanks a lot really i was just stupid because i used that "/var/www/devData/ " but i didn't specify the name of the folder ;) – Edivad Jul 30 '12 at 14:19
1

The following code may reduce complexity with a php function to find the current directory.

 $thisdir = getcwd(); //current directory
    if(mkdir($thisdir ."/mydir", 0777 ))
    {
       echo "Directory has been created successfully...";
    }
    else
    {
       echo "Failed to create directory...";

    }

this stuff works good. make sure your parent directory is writable.

0

you might have no rights to do this try

 <?php
      $new_folder = "var/www/devData";
      if(!mkdir($new_folder,0777,true)) { // true = for recrusive createing
         echo "Can't creating ".$_SERVER['DOCUMENT_ROOT']."/".$new_folder;
      }
      else 
         echo "Successful creating ".$_SERVER['DOCUMENT_ROOT']."/".$new_folder;
 ?>
donald123
  • 5,638
  • 3
  • 26
  • 23
  • the path is not good but how can i check the correct path? because i have to create that dir in apache but in another dir of my current work directory – Edivad Jul 30 '12 at 13:49
0

read this

http://php.net/manual/en/function.mkdir.php

try this:-

<?php
// Desired folder structure
$structure = './depth1/depth2/depth3/';

// To create the nested structure, the $recursive parameter 
// to mkdir() must be specified.

if (!mkdir($structure, 0, true)) {
    die('Failed to create folders...');
}

// ...
?>
Abid Hussain
  • 7,724
  • 3
  • 35
  • 53
0

First make sure that the owner of your file.php script has write-access to the directory /var/www/. is_writable() will tell you.

Then: You are using a relative path, so the script tries to create a directory relative to its own location. You would need to

a) use an absolute path (/var/www/...)

or

b) use a relative path but without the path file.php itself is located in (e.g.: file.php is = /var/www/file.php, the use mkdir("devData/",0777);).

Also: It of course says "created" because you print it out no matter what. If you want to have it say "created" only if nothing goes wrong, try

if (mkdir("devData/",0777)) {
    echo "created";
}
else {
    echo "something went wrong...";
}
MiDo
  • 1,057
  • 1
  • 7
  • 11