Link to home
Start Free TrialLog in
Avatar of hankknight
hankknightFlag for Canada

asked on

file_put_contents for file in new directory

This does NOT work if unless all subdirectories already exist.

          file_put_contents('/newdir/subdir/file.txt','content');

I want it to create all the necessary folders.
ASKER CERTIFIED SOLUTION
Avatar of Rob Siklos
Rob Siklos
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hankknight

ASKER

Thanks for the idea but this needs to be dynamic.

The folder might exist and it might not.
Avatar of hielo
You can save the directory onto a variable and the filename onto another:
$dir ='/newdir/subdir';
$file = 'file.txt'

if( !file_exists($dir) )
{
 mkdir($dir,0777,TRUE);
}
 file_put_contents( $dir . "/". $file,'content');
Thanks, hielo, but your idea does not work for me either.

There could be a page 10 directories deep, so 10 directories would need to be created.

     /dir1/dir2/dir3/dir4/dir5/dir6/file.txt
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial