Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create file and folders recursively

Tags:

php

I got an array containing path names and file names

['css/demo/main.css', 'home.css', 'admin/main.css','account']

I want to create those files and folders if they are not existed yet. Overwrite them if they are already existed.

like image 732
angry kiwi Avatar asked Jul 11 '11 13:07

angry kiwi


People also ask

How do you create a recursive directory in Java?

mkdirs() method to create a collection of directories recursively. It will create a directory with all its necessary parent directories.

How do I make a folder recursive?

Using the mkdir() Method File class and define a method named file() which internally makes use of the mkdir() function to recursively create directories.

How do I make a directory recursive in Python?

makedirs() method in Python is used to create a directory recursively. That means while making leaf directory if any intermediate-level directory is missing, os. makedirs() method will create them all.

What is recursive folder?

Alternatively referred to as recursive, recurse is a term used to describe the procedure capable of being repeated. For example, when listing files in a Windows command prompt, you can use the dir /s command to recursively list all files in the current directory and any subdirectories.


2 Answers

For each of this paths you'll have to specific whether it is a file or a directory. Or you could make your script assume, that the path is pointing to a file when the basename (the last part of the path) contains a dot.

To create a directory recursively is simple:

mkdir(dirname($path), 0755, true); // $path is a file
mkdir($path, 0755, true);          // $path is a directory

0755 is the file permission expression, you can read about it here: http://ch.php.net/manual/en/function.chmod.php

like image 135
Philippe Gerber Avatar answered Sep 17 '22 17:09

Philippe Gerber


<?php
  function mkpath($path)
  {
    if(@mkdir($path) or file_exists($path)) return true;
    return (mkpath(dirname($path)) and mkdir($path));
  }
?>

This makes paths recursively.

like image 44
spraff Avatar answered Sep 16 '22 17:09

spraff