Home > Software design >  create all missing folders automatically on a given path
create all missing folders automatically on a given path

Time:11-14

I get some rows from mysql table
And want to put data from column story to a new-creating .txt file inside file-system
And gettint error - some folders in path - dont exists
Is there a way to create all missing folders automatically and finally create .txt file ?

foreach($rows as $row){
    $path = $row['pt'] . "/" . $row['title'] . ".txt";
    if(!is_file($path)){
        fopen($path, "w");
        file_put_contents($path, $row['story']);
    }
}

CodePudding user response:

just use php mkdir - mkdir

foreach($rows as $row){
    $path = $row['pt'] . "/" . $row['title'] . ".txt";
// make the folder, u can check if the folder exist, if not create it  
   mkdir($row['pt'] , 0777, true);
// put content
    if(!is_file($path)){
        fopen($path, "w");
        file_put_contents($path, $row['story']);
    }
}
  • Related