Page 365 - Beginning PHP 5.3
P. 365
Chapter 11: Working with Files and Directories
$filepath = PATH_TO_FILES . “/$filename”;
if ( file_exists( $filepath ) ) {
if ( file_put_contents( $filepath, $_POST[“fileContents”] ) === false )
die( “Couldn’t save file” );
displayFileList();
} else {
die( “File not found” );
}
}
function createFile() {
$filename = basename( $_POST[“filename”] );
$filename = preg_replace( “/[^A-Za-z0-9_\- ]/”, “”, $filename );
if ( !$filename ) {
displayFileList( “Invalid filename - please try again” );
return;
}
$filename .= “.txt”;
$filepath = PATH_TO_FILES . “/$filename”;
if ( file_exists( $filepath ) ) {
displayFileList( “The file $filename already exists!” );
} else {
if ( file_put_contents( $filepath, “” ) === false ) die( “Couldn’t create
file” );
chmod( $filepath, 0666 );
displayEditForm( “$filename” );
}
}
function displayPageHeader() {
? >
< !DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd” >
< html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”en” lang=”en” >
< head >
< title > A simple text editor < /title >
< link rel=”stylesheet” type=”text/css” href=”common.css” / >
< style type=”text/css” >
.error { background: #d33; color: white; padding: 0.2em; }
th { text-align: left; background-color: #999; }
th, td { padding: 0.4em; }
< /style >
< /head >
< body >
< h1 > A simple text editor < /h1 >
< ?php
}
? >
327
9/21/09 9:10:21 AM
c11.indd 327
c11.indd 327 9/21/09 9:10:21 AM