Php: creating directory

190 Views Asked by At

I am trying to create folder and sub folder on website. Code is pretty simple. Not sure why does not work.

<?php

$domain = "officeactionuspto.com";

mkdir(($_SERVER["DOCUMENT_ROOT"].'/crc/website_templates/client_files/'.$domain), 0777, true);
mkdir(($_SERVER["DOCUMENT_ROOT"].'/crc/website_templates/client_files/'.$domain.'/images'), 0777, true);


$folder= $_SERVER["DOCUMENT_ROOT"].'/crc/website_templates/client_files/'.$domain.'/images';


if(is_dir($folder))
  {
  echo ("$folder is a directory");
  }
else
  {
  echo ("$folder is not a directory");
  }

?>
2

There are 2 best solutions below

3
Ramsay Smith On

You don't need to use the entire, absolute filename. You just need to use the path relative to the folder where the script being executed is located.

Although I don't know your file structure, lets pretend that the PHP script is in the crc folder: Your command would be: mkdir(('/website_templates/client_files/'.$domain), 0777, true);

EDIT: With the recursive parameter, you can create the images subfolder in the same command.

2
Murilo Azevedo On

You don't have to use absolute path to create a directory.

You can just do it with following code:

mkdir('images', 0777, true);


$folder= 'images';

if(is_dir($folder)){
  echo ("$folder is a directory");
}else{
  echo ("$folder is not a directory");
}

you can also get the absolute path after created, if you desired:

$full_path = realpath('images');

PS: I'm supposing you are executing this code on /index.php, if was on another different structure, you need to write the relative path for it.

EDIT: I tested and eliminate a parentheses on mkdir and works.