Affordable Web Hosting with Excellent Customer Support internet connection free month hosting

PHP File Write

Writing a file with php requires defining the target file, opening the file for writing, performing the writing to disk, and closing the file. Note: permissions must be set on this script for write access to disk (chmod 0755). Read about File Permissions.

Basic script:

$PATH = $_ENV['DOCUMENT_ROOT']; #get server path
$save_path=$PATH.'/yourdir'; #define target path

$data='This is what is written to the file.\nThis is the second line.';

$temp='file.tmp'; #define temporary target name
$dest='file.txt'; #define final destination target name

$fp = fopen($save_path.'/'.$temp, "w", 0); #open for writing
  fputs($fp, $data); #write all of $data to our opened file
  fclose($fp); #close the file
}

#rename tmp file to dest file
rename($save_path.'/'.$temp,$save_path.'/'.$dest);