File Writing Issue

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I am writing a file using PHP and all is great thus far:

$file_contents =     "<?php \n".
                    "$file_inc_news_id = '" .$id. "' \n" .
                    "include( $_SERVER['DOCUMENT_ROOT'] . '/news/news_item.php' );
\n" .
$filename = $_SERVER['DOCUMENT_ROOT'] . "/" .date('his'). ".php";
$fp = fopen($filename , "w");  //to write a new file
fwrite($fp , $file_contents );

However I want to add the facility for the file to be written into a
sub directory that may or may not exist - if the folder exists then it
works fine - if it doesn't then it throws an error.

Without having to write a section of code to check if the folder
exists, is there a way of forcing PHP to build any required folders for

Re: File Writing Issue

If you want to check if the sub directoy exists and if not create it
then its only two lines of code:

if (!file_exists($dir))

If you are using php 5 there is a recursive param added. Never tried it

Re: File Writing Issue

Thanks mate - that rocks my world!

Site Timeline