Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Ch3(working with file)

47 views

Published on

chhom karath

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Ch3(working with file)

  1. 1. Working with Files
  2. 2. include() • The include() statement enables you to incorporate files into your PHP documents – <?php include("11.php"); ?> – <?php $addResult = include("a.php"); print "The include file returned $addResult"; ?> – a.php <?php $retval = ( 4 + 4 ); return $retval; ?> – EX: <?php for ( $x=1; $x<=3; $x++ ) { $incfile = "file".$x.".txt"; include( "$incfile" ); } ?>
  3. 3. require_once()/ include_once • EX: <?php for ( $x=1; $x<=3; $x++ ) { $incfile = "file".$x.".txt"; include_once( "$incfile" );// require_once( "$incfile" ); } for ( $x=1; $x<=3; $x++ ) { $incfile = "file".$x.".txt"; include_once( "$incfile" ); } ?>
  4. 4. Testing Files • You can test for the existence of a file with the file_exists() function, return true and false. EX: <?php if ( file_exists ("12.php") ) { print "The file exists!";} else { print "Not found file";} ?> • You can confirm that the entity you are testing is a file, as opposed to a directory, with the is_file() function EX: <?php if ( is_file( "file1.txt" ) ) { print "test.txt is a file!"; } ?>
  5. 5. EX: is directory te? <?php if ( is_dir( "a" ) ) { print "a is a directory"; } else { print "a is not directory te!"; } ?> EX: 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"; } print "The size of test.txt is. "; print filesize( "test.txt" ); EX: $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 EX: $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 EX: <?php $ctime = filectime( "file1.txt" );print "test.txt was last changed on ";print date("D d M Y g:i A", $ctime);?>
  6. 6. Creating and Deleting Files • touch("myfile.txt"); //create file • unlink("myfile.txt"); //remove file The most common modes are read ('r'), write ('w'), and append ('a'). fopen() returns a file resource you will later use to work with the open file. • $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"); • fclose( $fp ); If you are writing a binary file on a Windows system, you should add a 'b' flag to your fopen() mode argument. • $fp = fopen( "binary_file", "wb" ); and read them like this: • $fp = fopen( "binary_file", "rb" );
  7. 7. Reading from Files Reading Lines from a File with fgets() and feof() •feof($line = fgets( $fp, 1024 ); // where $fp is the file resource returned by fopen() •$fp ); // where $fp is the file resource returned by fopen()
  8. 8. Reading Arbitrary Amounts of Data from a File with fread() •Rather than reading text by the line, you can choose to read a file in arbitrarily defined chunks. •EX: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <title>Listing 11.10 Reading a File with fread()</title> </head> <body> <div> <?php $filename = "test.txt"; $fp = fopen( $filename, "r" ) or die("Couldn't open $filename"); while ( ! feof( $fp ) ) { $chunk = fread( $fp,1 ); print "$chunk<br/>"; } ?> </div> </body> </html>
  9. 9. Moving Around a File with fseek() •EX: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <title> Listing 11.11 Moving Around a File with fseek()</title> </head> <body> <div> <?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; ?> </div> </body> </html>
  10. 10. Reading Characters with fgetc() •fgetc() is similar to fgets() except that it returns only a single character from a file every time it is called. •fgetc() doesn't require a length argument. •$char = fgetc( $fp ); •EX: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <title>Listing 11.12 Reading Characters with fgetc()</title> </head> <body> <div> <?php $filename = "test.txt"; $fp = fopen( $filename, "r" ) or die("Couldn't open $filename"); while ( ! feof( $fp ) ) { $char = fgetc( $fp ); print "$char<br/>"; } ?> </div> </body></html>
  11. 11. Reading the Contents of a File with file_get_contents() •EX: <?php $contents = file_get_contents( "test.txt" ); print $contents; ?> •EX: <?php $file_array = file( "test.txt" ); $contents = implode( $file_array ); print $contents; ?>
  12. 12. Writing or Appending to a File • Writing to a File with fwrite() or fputs() • $fp = fopen( "test.txt", "w" );//write to a file • $fp = fopen( "test.txt", "a" ); //file already exists, any prior content is destroyed and replaced by the data you write. • EX: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1- strict.dtd"> <html> <head> <title>Listing 11.13 Writing and Appending to a File</title> </head> <body> <div> <?php $filename = "test2.txt"; print "Writing to $filename<br/>"; $fp = fopen( $filename, "w" ) or die("Couldn't open $filename"); fwrite( $fp, "Hello worldn" ); fclose( $fp ); print "Appending to $filename<br/>"; $fp = fopen( $filename, "a" ) or die("Couldn't open $filename"); fputs( $fp, "And another thingn" ); fclose( $fp ); ?> </div> </body> </html>
  13. 13. •Writing Data to a File with file_put_contents() •file_put_contents( "test2.txt", "Hello worldn" ); •file_put_contents( "test2.txt", "And another thingn", FILE_APPEND ); •Locking Files with flock() •EX: $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 );

×