8.Files

1. Including Files
<?php
include("listing11.4.php");
?>
<?php
print "I have been included!!<BR>";
print "But now I can add up.. 4 + 4 = ".(4 + 4);
?>


2. include that returns a value
<?php 
$addResult = include("listing11.6.php"); 
print "The include file returned $addResult"; 
?> 
<?php 
$retval = ( 4 + 4 ); 
return $retval; 
?> 
This HTML should never be displayed because it comes after a return statement! 


3. Using Include within control structures
$test = false; 
if ( $test ) 
{ include( "a_file.txt" ); // won't be included } 


4. include() inside the loop
<?php 
for ( $x=1; $x<=3; $x++ ) { 
$incfile = "incfile".$x.".txt"; 
print "<p>"; 
print "Attempting include $incfile<br/>"; 
include( "$incfile" ); 
print "</p>"; 

?>


5. Avoiding duplicated include()
include_once() – works like include() but would not allow inclusion of the same file more than once


6. Managing Larger Projects
Set the php.ini to:
include_path=".:/home/mary/php_lib:/usr/local/lib/php" 


include_path option in an .htaccess file:
php_value include_path /home/mary/php_lib 


Runtime setting up of php.ini
ini_set("include_path","/home/mary/php_lib"); 
set_include_path( "/home/mary/php_lib" ); 


7. Testing Files
Checking for Existence with file_exists()
if ( file_exists ("test.txt") ) {  print "The file exists!";}


8. A File or a Directory?
if ( is_file( "test.txt" ) ) {  print "test.txt is a file!";}
if ( is_dir( "/tmp" ) ) {  print "/tmp is a directory";} 


9. Checking the Status of a File
if ( is_readable( "test.txt" ) ) {  print "test.txt is readable";}
if ( is_writable( "test.txt" ) ) {  print "test.txt is writable";}
if ( is_executable( "test.txt" ) {  print "test.txt is executable";} 


10. Determining File Size with filesize()
print "The size of test.txt is. ";print filesize( "test.txt" ) 


11. Date Information About a File
atime = fileatime( "test.txt" );
print "test.txt was last accessed on ";
print date("D d M Y g:i A", $atime);
// Sample output: Tue 19 Aug 2003 4:26 PM


$mtime = filemtime( "test.txt" );
print "test.txt was last modified on ";
print date("D d M Y g:i A", $mtime);
// Sample output: Tue 19 Aug 2003 4:26 PM


$ctime = filectime( "test.txt" );
print "test.txt was last changed on ";
print date("D d M Y g:i A", $ctime);
// Sample output: Tue 19 Aug 2003 4:26 PM 


12. Testing of File  - An example
<?php 
function outputFileTestInfo( $file ) { 
if ( ! file_exists( $file ) ) { 
print "$file does not exist<br/>"; 
return;

print "$file is ".( is_file( $file )?"":"not ")."a file<br/>"; 
print "$file is ".( is_dir( $file )?"":"not ")."a directory<br/>"; 
print "$file is ".( is_readable( $file )?"":"not ")."readable<br/>"; 
print "$file is ".( is_writable( $file )?"":"not ")."writable<br/>"; 
print "$file is ".( is_executable( $file )?"":"not")."executable<br/>"; 
print "$file is ".( filesize($file))." bytes<br/>"; 
print "$file was accessed on ".date( "D d M Y g:i A", fileatime( $file ) )."<br/>"; 
print "$file was modified on " .date( "D d M Y g:i A", filemtime( $file ) )."<br/>"; 
print "$file was changed on " .date( "D d M Y g:i A", filectime( $file ) )."<br/>"; 

?> 


13. Creating and Deleting Files 
touch("myfile.txt"); 
unlink("myfile.txt"); 


14. Opening a File for Writing, Reading, or Appending 
$fp = fopen( "test.txt", 'r' );
$fp = fopen( "test.txt", 'w' );
$fp = fopen( "test.txt", 'a' );
if ( $fp = fopen( "test.txt", "w" ) ) {  // do something with $fp}
( $fp = fopen( "test.txt", "w" ) ) or die ("Couldn't open file, sorry"); 
fp = fopen( "binary_file", "wb" ); //and read them like this:
$fp = fopen( "binary_file", "rb" ); 
fclose( $fp ); 


15. Reading from Files 
Reading Lines from a File with fgets() and feof() 
$line = fgets( $fp, 1024 ); // where $fp is the file resource returned by fopen() 
feof( $fp ); // where $fp is the file resource returned by fopen() 


16. Reading from a File – An Example
<?php 
$filename = "test.txt"; 
$fp = fopen( $filename, "r" ) or die("Couldn't open $filename"); 
while ( ! feof( $fp ) ) { 
$line = fgets( $fp, 1024 ); 
print "$line<br/>"; 

?> 


17. Reading Arbitrary Amounts of Data 
$chunk = fread( $fp, 16 ); 
<?php 
$filename = "test.txt"; 
$fp = fopen( $filename, "r" ) or die("Couldn't open $filename"); 
while ( ! feof( $fp ) ) { 
$chunk = fread( $fp,16 ); 
print "$chunk<br/>"; 

?> 


18. File Pointer Position
fseek( $fp, 64 ); 
<?php 
$filename = "test.txt"; 
$fp = fopen( $filename, "r" ) or die("Couldn't open $filename"); 
$fsize = filesize($filename); 
$halfway = (int)( $fsize / 2 ); 
print "Halfway point: $halfway <br/>\n"; 
fseek( $fp, $halfway ); 
$chunk = fread( $fp, ($fsize - $halfway) ); 
print $chunk; 
?> 


19. Reading Characters from a File
$char = fgetc( $fp ); 
<?php 
$filename = "test.txt"; 
$fp = fopen( $filename, "r" ) or die("Couldn't open $filename"); 
while ( ! feof( $fp ) ) { 
$char = fgetc( $fp ); 
print "$char<br/>"; 

?> 


20. Reading all file content
$contents = file_get_contents( "test.txt" );
$file_array = file( "test.txt" );//older
$contents = implode( $file_array ); 


21. Writing or Appending to a File 
$fp = fopen( "test.txt", "w" ); 
$fp = fopen( "test.txt", "a" ); 
fwrite( $fp, "hello world" ); 
fputs( $fp, "hello world" ); 


Write to a File – An example 
<?php 
$filename = "test2.txt"; 
print "Writing to $filename<br/>"; 
$fp = fopen( $filename, "w" ) or die("Couldn't open $filename"); 
fwrite( $fp, "Hello world\n" ); 
fclose( $fp ); 
print "Appending to $filename<br/>"; 
$fp = fopen( $filename, "a" ) or die("Couldn't open $filename"); 
fputs( $fp, "And another thing\n" ); 
fclose( $fp ); 
?> 


22. Writing Data to a File – Another twist 
file_put_contents( "test2.txt", "Hello world\n" ); 
file_put_contents( "test2.txt", "And another thing\n", FILE_APPEND ); 


23. Locking Files with flock() 
LOCK_SH (Shared)
-Allows other processes to read the file but prevents writing (used when reading a file)
LOCK_EX (Exclusive)
-Prevents other processes from either reading from or writing to a file (used when writing to a file)
LOCK_UN (Release)
-Releases a shared or exclusive lock


24. Locking Files – An Example
$fp = fopen( "test.txt", "a" ) or die("couldn't open"); 
flock( $fp, LOCK_EX ); 
// exclusive lock // write to the file
 flock( $fp, LOCK_UN );
// release the lock fclose( $fp ); 


25. Working with Directories 
mkdir( "testdir", 0777 ); 
// global read/write/execute permissions 
mkdir( "testdir", 0755 ); 
// world and group: read/execute only // owner: read/write/execute 
rmdir( "testdir" ); 
Removing a directory


26. Opening a Directory for Reading 
<?php 
$dirname = "."; 
$dh = opendir( $dirname ); 
while ( ! is_bool( $file = readdir( $dh )) ) { 
if ( is_dir( "$dirname/$file" ) ) {
print "(D) "; 

print "$file<br/>"; 

closedir( $dh ); 
?> 

No comments:

Post a Comment