How to create multiple folders with mkdir

Asked

Viewed 1,114 times

3

Hello, I would like to know how to create multiple folders at once, for example with that 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 -> Opening -> Social Contract, in case google is the parent folder and opening is inside it, and social contract is another folder that is inside opening, which is inside google, got a little confused but would be something like this:

Google Companies Opening Social contract

EDIT:

I put together something more or less like this but still it makes a mistake, it doesn’t create the folders inside, it creates outside like 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);
?>

4 answers

1

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

This component allows you to do these tasks of creating directories, changing permissions, moving files, etc..., in a way independent of file system and operating system. So your code will work independently of running on Linux/Windows/BSD/etc.

To install, use the command:

composer require symfony/filesystem

A basic example of use:

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 he 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 in this link.

1


It may be because of the intercalation 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);

?>

0

Set the third parameter of the function with value true.

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

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

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

The code above is an illustrative example.

0

Your code had concatenation problems in the $pathNameX variables, I fixed 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);
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.