Jump to content

Archived

This topic is now archived and is closed to further replies.

legohead6

creating a new file

Recommended Posts

Look in the manual at the section on filesystems. http://www.php.net/manual/en/ref.filesystem.php

Look at the fopen(), fwrite(), and fclose() topics.

Ken

Share this post


Link to post
Share on other sites
[url=http://php.net/fopen]fopen[/url].

Share this post


Link to post
Share on other sites
Look at the manual page. There are options to the fopen() function which will let you create a file.

Ken

Share this post


Link to post
Share on other sites
<?php
$filename = 'test.txt';
$somecontent = "Add this to the file\n";

// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {

  // In our example we're opening $filename in append mode.
  // The file pointer is at the bottom of the file hence
  // that's where $somecontent will go when we fwrite() it.
  if (!$handle = fopen($filename, 'a')) {
        echo "Cannot open file ($filename)";
        exit;
  }

  // Write $somecontent to our opened file.
  if (fwrite($handle, $somecontent) === FALSE) {
      echo "Cannot write to file ($filename)";
      exit;
  }
 
  echo "Success, wrote ($somecontent) to file ($filename)";
 
  fclose($handle);

} else {
  echo "The file $filename is not writable";
}
?>

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.