How to loop throught folders and subfolders and create using Laravel?












0















Hi need help in creating nested folders.



I currently have a folder called images and i would like to clone the folders and save it in a different folder call backup. and the backup will only happen when user clicks backup.



For example:



- Images
- Car
- A
- A1
- A1-1
- A2
- B
- Van


How can i write the code so that it will create the folders?



Currently i have done this so how can i do it?



public function sync(Request $request)
{
$arr = ;
$folderToSync = $request->input('folderName');

$originalPath = public_path($folderToSync);
$newFolderPath = public_path('S3/'.$folderToSync);

$this->createFolder($newFolderPath); // create the selected folder

$directories = $this->getAllFolders($originalPath); // getting all folders under the original path

$this->folder($directories, $newFolderPath, $originalPath);

dd('end');
}

public function createFolder($path)
{
if (!is_dir($path)) {
@mkdir($path, 0777, true);
}
}

public function folder($directories, $newFolderPath, $originalPath)
{
foreach ($directories as $directory) {
$newPath = $newFolderPath.'/'.$directory;
$oriPath = $originalPath.'/'.$directory;

$this->createFolder($newPath);
$subFolders = $this->getAllFolders($oriPath);

if ($subFolders) {
$this->subfolder($subFolders, $newPath);
}
}
}

public function subfolder($directories, $path)
{
foreach ($directories as $directory) {
$this->createFolder($path.'/'.$directory);
}
}

public function getAllFolders($path)
{
return array_map('basename', File::directories($path));
}

public function getAllFiles($path)
{
return;
}


but its not creating the subfolders. How can i modify it ?



i would run the code every week and i want to check also which folder have been created and which have not been created. if the folder does not exist then create the folder.










share|improve this question

























  • Possible duplicate of List all the files and folders in a Directory with PHP recursive function

    – Matt Komarnicki
    Nov 23 '18 at 5:38











  • The 3rd parameter of mkdir is "recursive" so it will create all parent folders of the folder you're trying to create. Is that not working? Also why use @ in front of mkdir? What are you suppressing?

    – apokryfos
    Nov 23 '18 at 7:05


















0















Hi need help in creating nested folders.



I currently have a folder called images and i would like to clone the folders and save it in a different folder call backup. and the backup will only happen when user clicks backup.



For example:



- Images
- Car
- A
- A1
- A1-1
- A2
- B
- Van


How can i write the code so that it will create the folders?



Currently i have done this so how can i do it?



public function sync(Request $request)
{
$arr = ;
$folderToSync = $request->input('folderName');

$originalPath = public_path($folderToSync);
$newFolderPath = public_path('S3/'.$folderToSync);

$this->createFolder($newFolderPath); // create the selected folder

$directories = $this->getAllFolders($originalPath); // getting all folders under the original path

$this->folder($directories, $newFolderPath, $originalPath);

dd('end');
}

public function createFolder($path)
{
if (!is_dir($path)) {
@mkdir($path, 0777, true);
}
}

public function folder($directories, $newFolderPath, $originalPath)
{
foreach ($directories as $directory) {
$newPath = $newFolderPath.'/'.$directory;
$oriPath = $originalPath.'/'.$directory;

$this->createFolder($newPath);
$subFolders = $this->getAllFolders($oriPath);

if ($subFolders) {
$this->subfolder($subFolders, $newPath);
}
}
}

public function subfolder($directories, $path)
{
foreach ($directories as $directory) {
$this->createFolder($path.'/'.$directory);
}
}

public function getAllFolders($path)
{
return array_map('basename', File::directories($path));
}

public function getAllFiles($path)
{
return;
}


but its not creating the subfolders. How can i modify it ?



i would run the code every week and i want to check also which folder have been created and which have not been created. if the folder does not exist then create the folder.










share|improve this question

























  • Possible duplicate of List all the files and folders in a Directory with PHP recursive function

    – Matt Komarnicki
    Nov 23 '18 at 5:38











  • The 3rd parameter of mkdir is "recursive" so it will create all parent folders of the folder you're trying to create. Is that not working? Also why use @ in front of mkdir? What are you suppressing?

    – apokryfos
    Nov 23 '18 at 7:05
















0












0








0








Hi need help in creating nested folders.



I currently have a folder called images and i would like to clone the folders and save it in a different folder call backup. and the backup will only happen when user clicks backup.



For example:



- Images
- Car
- A
- A1
- A1-1
- A2
- B
- Van


How can i write the code so that it will create the folders?



Currently i have done this so how can i do it?



public function sync(Request $request)
{
$arr = ;
$folderToSync = $request->input('folderName');

$originalPath = public_path($folderToSync);
$newFolderPath = public_path('S3/'.$folderToSync);

$this->createFolder($newFolderPath); // create the selected folder

$directories = $this->getAllFolders($originalPath); // getting all folders under the original path

$this->folder($directories, $newFolderPath, $originalPath);

dd('end');
}

public function createFolder($path)
{
if (!is_dir($path)) {
@mkdir($path, 0777, true);
}
}

public function folder($directories, $newFolderPath, $originalPath)
{
foreach ($directories as $directory) {
$newPath = $newFolderPath.'/'.$directory;
$oriPath = $originalPath.'/'.$directory;

$this->createFolder($newPath);
$subFolders = $this->getAllFolders($oriPath);

if ($subFolders) {
$this->subfolder($subFolders, $newPath);
}
}
}

public function subfolder($directories, $path)
{
foreach ($directories as $directory) {
$this->createFolder($path.'/'.$directory);
}
}

public function getAllFolders($path)
{
return array_map('basename', File::directories($path));
}

public function getAllFiles($path)
{
return;
}


but its not creating the subfolders. How can i modify it ?



i would run the code every week and i want to check also which folder have been created and which have not been created. if the folder does not exist then create the folder.










share|improve this question
















Hi need help in creating nested folders.



I currently have a folder called images and i would like to clone the folders and save it in a different folder call backup. and the backup will only happen when user clicks backup.



For example:



- Images
- Car
- A
- A1
- A1-1
- A2
- B
- Van


How can i write the code so that it will create the folders?



Currently i have done this so how can i do it?



public function sync(Request $request)
{
$arr = ;
$folderToSync = $request->input('folderName');

$originalPath = public_path($folderToSync);
$newFolderPath = public_path('S3/'.$folderToSync);

$this->createFolder($newFolderPath); // create the selected folder

$directories = $this->getAllFolders($originalPath); // getting all folders under the original path

$this->folder($directories, $newFolderPath, $originalPath);

dd('end');
}

public function createFolder($path)
{
if (!is_dir($path)) {
@mkdir($path, 0777, true);
}
}

public function folder($directories, $newFolderPath, $originalPath)
{
foreach ($directories as $directory) {
$newPath = $newFolderPath.'/'.$directory;
$oriPath = $originalPath.'/'.$directory;

$this->createFolder($newPath);
$subFolders = $this->getAllFolders($oriPath);

if ($subFolders) {
$this->subfolder($subFolders, $newPath);
}
}
}

public function subfolder($directories, $path)
{
foreach ($directories as $directory) {
$this->createFolder($path.'/'.$directory);
}
}

public function getAllFolders($path)
{
return array_map('basename', File::directories($path));
}

public function getAllFiles($path)
{
return;
}


but its not creating the subfolders. How can i modify it ?



i would run the code every week and i want to check also which folder have been created and which have not been created. if the folder does not exist then create the folder.







php laravel






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 7:50









Julian Stark

1,2681426




1,2681426










asked Nov 23 '18 at 5:35









Daljit SinghDaljit Singh

112




112













  • Possible duplicate of List all the files and folders in a Directory with PHP recursive function

    – Matt Komarnicki
    Nov 23 '18 at 5:38











  • The 3rd parameter of mkdir is "recursive" so it will create all parent folders of the folder you're trying to create. Is that not working? Also why use @ in front of mkdir? What are you suppressing?

    – apokryfos
    Nov 23 '18 at 7:05





















  • Possible duplicate of List all the files and folders in a Directory with PHP recursive function

    – Matt Komarnicki
    Nov 23 '18 at 5:38











  • The 3rd parameter of mkdir is "recursive" so it will create all parent folders of the folder you're trying to create. Is that not working? Also why use @ in front of mkdir? What are you suppressing?

    – apokryfos
    Nov 23 '18 at 7:05



















Possible duplicate of List all the files and folders in a Directory with PHP recursive function

– Matt Komarnicki
Nov 23 '18 at 5:38





Possible duplicate of List all the files and folders in a Directory with PHP recursive function

– Matt Komarnicki
Nov 23 '18 at 5:38













The 3rd parameter of mkdir is "recursive" so it will create all parent folders of the folder you're trying to create. Is that not working? Also why use @ in front of mkdir? What are you suppressing?

– apokryfos
Nov 23 '18 at 7:05







The 3rd parameter of mkdir is "recursive" so it will create all parent folders of the folder you're trying to create. Is that not working? Also why use @ in front of mkdir? What are you suppressing?

– apokryfos
Nov 23 '18 at 7:05














1 Answer
1






active

oldest

votes


















0














I would have a look at the storage api from the Laravel documentation: https://laravel.com/docs/5.7/filesystem#directories



To acquire folders and subfolders:



// Recursive...
$directories = Storage::allDirectories($directory);


Creating a new directory:



Storage::makeDirectory($directory);


Storing files:



Storage::put('file.jpg', $contents);

Storage::put('file.jpg', $resource);


The put method will take either the file contents or a resource.



Don't forget to include the Storage facade:



use IlluminateSupportFacadesStorage;





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53441123%2fhow-to-loop-throught-folders-and-subfolders-and-create-using-laravel%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I would have a look at the storage api from the Laravel documentation: https://laravel.com/docs/5.7/filesystem#directories



    To acquire folders and subfolders:



    // Recursive...
    $directories = Storage::allDirectories($directory);


    Creating a new directory:



    Storage::makeDirectory($directory);


    Storing files:



    Storage::put('file.jpg', $contents);

    Storage::put('file.jpg', $resource);


    The put method will take either the file contents or a resource.



    Don't forget to include the Storage facade:



    use IlluminateSupportFacadesStorage;





    share|improve this answer




























      0














      I would have a look at the storage api from the Laravel documentation: https://laravel.com/docs/5.7/filesystem#directories



      To acquire folders and subfolders:



      // Recursive...
      $directories = Storage::allDirectories($directory);


      Creating a new directory:



      Storage::makeDirectory($directory);


      Storing files:



      Storage::put('file.jpg', $contents);

      Storage::put('file.jpg', $resource);


      The put method will take either the file contents or a resource.



      Don't forget to include the Storage facade:



      use IlluminateSupportFacadesStorage;





      share|improve this answer


























        0












        0








        0







        I would have a look at the storage api from the Laravel documentation: https://laravel.com/docs/5.7/filesystem#directories



        To acquire folders and subfolders:



        // Recursive...
        $directories = Storage::allDirectories($directory);


        Creating a new directory:



        Storage::makeDirectory($directory);


        Storing files:



        Storage::put('file.jpg', $contents);

        Storage::put('file.jpg', $resource);


        The put method will take either the file contents or a resource.



        Don't forget to include the Storage facade:



        use IlluminateSupportFacadesStorage;





        share|improve this answer













        I would have a look at the storage api from the Laravel documentation: https://laravel.com/docs/5.7/filesystem#directories



        To acquire folders and subfolders:



        // Recursive...
        $directories = Storage::allDirectories($directory);


        Creating a new directory:



        Storage::makeDirectory($directory);


        Storing files:



        Storage::put('file.jpg', $contents);

        Storage::put('file.jpg', $resource);


        The put method will take either the file contents or a resource.



        Don't forget to include the Storage facade:



        use IlluminateSupportFacadesStorage;






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 6:00









        adamadam

        917811




        917811






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53441123%2fhow-to-loop-throught-folders-and-subfolders-and-create-using-laravel%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Wiesbaden

            Marschland

            Dieringhausen