File Writing Issue

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • thehuby

    File Writing Issue

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

    $file_contents = "<?php \n".
    "\$file_inc_new s_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 );
    fclose($fp);

    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
    me?

  • Sean

    #2
    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))
    mkdir($dir)

    If you are using php 5 there is a recursive param added. Never tried it
    myself: http://ie2.php.net/mkdir

    Comment

    • thehuby

      #3
      Re: File Writing Issue

      Thanks mate - that rocks my world!

      Comment

      Working...