September 9th, 2007 at

Create a file on your server using php

in: Scripts

This function allows you to save a file (with correct folder permissions) on your web server or web site with any content you choose.  This is ideal for adding pages dynamically to your site or for error reporting.

The function uses two inputs:

$location - the file location including the file name and extension e.g. "folder/file.html"
$content - this is the content intended for the file e.g. "<html><body>Hello world</body></html>"

Please be aware you'll need to modify folder permissions for this to work.

The function uses fopen and fwrite functions.

######## start script

function createFile($location, $content) {
 
  $ourFileHandle = fopen($location, 'w') or die("can't open file");
  fclose($ourFileHandle);
  
  $fh = fopen($location, 'w') or die("can't open file");
  
  $stringData = $content;
  fwrite($fh, $stringData);

  fclose($fh);
 
}

createFile("testcreateFile.txt", "this is the content");

######## finish script

Download the complete script here

 

RSS feed for comments on this post | TrackBack URI