How to create multiple folders with mkdir

2

Hello, I would like to know how to create multiple folders at once, for example, with this code:

<?php
        $empresa = "Google";
        $tipo = "Abertura";
        $nome = "Contrato Social";

        mkdir('$empresa/$tipo/$nome');
?>

Now with this data I would like to create in this case 3 folders that would be the  Google - > Aperture - > Social Contract, in case google is the mother folder and opening is inside it, and social contract is another folder that is inside opening, which is inside google, it was kind of confusing but it would look something like this:

Business \ Google \ Opening \ Social Contract

EDIT:

I've done something like this, but it's still an error, it does not create the folders inside, it creates out type Joaquim Sauro.FGTS

<?php
$nome_user = "Joaquim Sauro";
$tipo = "FGTS";
$categoria = "Abertura";
$pdf = "pdf.pdf";

$pathName = "html/empresas/" . $nome_user;

   mkdir($pathName,0777,true); 
    echo "OK1";

$pathName2 = "html/empresas/.$nome_user."/"" . $tipo;

    mkdir($pathName2,0777,true); 
     echo "OK1.1";

$pathName3 = "html/empresas/.$nome_user."/".$tipo."/"" . $categoria;

    mkdir($pathName3,0777,true);
     echo "OK1.2";

move_uploaded_file($pdf,$pathName3);
?>
    
asked by anonymous 23.02.2017 / 17:28

4 answers

1

It may be because of the interleaving of texts with variables, try the code below:

<?php
$nome_user = "Joaquim Sauro";
$tipo = "FGTS";
$categoria = "Abertura";
$pdf = "pdf.pdf";

$pathName = "html/empresas/{$nome_user}/{$tipo}/{$categoria}";
mkdir($pathName, 0777, true);
echo "OK";

move_uploaded_file($pdf, $pathName);

? >

    
23.02.2017 / 20:25
1

In addition to the answers already mentioned, if you use composer and the components of Symfony you can use FileSystem .

This component allows you to do these tasks of creating directories, change permissions, move files, etc ..., in an independent file system and operating system. So your code will work regardless of whether you're running Linux / Windows / BSD / etc.

To install, use the command:

composer require symfony/filesystem

A basic example of usage:

include __DIR__ . '/vendor/autoload.php';

use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;

$fs = new Filesystem();

try {
    $fs->mkdir('empresa/tipo/nome');
    echo "deu tudo certo :)";
} catch (IOExceptionInterface $e) {
    echo "Um erro ocorreu ao gerar os diretorios ".$e->getPath();
}

With this command it will automatically create the three directories within the current folder. If the directories already exist, the routine proceeds normally without giving any error.

You can look at the official documentation this link.

    
24.02.2017 / 12:53
0

Set the third parameter of the function with value true .

This parameter indicates that non-existing directories will be created recursively.

mkdir('foo/bar/folder/', 0777, true);

Of course, in order to inform the third argument, it is necessary to define the second argument referring to read and write permissions. (chmod)

The code above is an illustrative example.

    
23.02.2017 / 17:52
0

Your code had concatenation problems in the $ pathNameX variables, I've corrected and reused the variables for ease.

<?php
$nome_user = "Joaquim Sauro";
$tipo      = "FGTS";
$categoria = "Abertura";
$pdf       = "pdf.pdf";

$pathName = "html/empresas/" . $nome_user;
mkdir($pathName,0777,true); 
echo "OK1";

$pathName2 = $pathName . "/" . $tipo;
mkdir($pathName2,0777,true); 
echo "OK1.1";

$pathName3 = $pathName2 . "/" . $categoria;
mkdir($pathName3,0777,true);
echo "OK1.2";

move_uploaded_file($pdf,$pathName3);
?>
    
23.02.2017 / 21:15