If you think that this site is helpful, please recommend your friends to visit our site.
How to read file, write file, and append file in php?
The following is the code to read file, write file, and append file in php:
1. How to write file in php?
-------------------------------------------------------------------------------- < ?php $file = "test.txt"; $f_handle = fopen($myFile, 'w') or die("can't open file"); $inputData = "this is a test\n"; fwrite(f_handle, $inputData); fclose(f_handle); ? > -------------------------------------------------------------------------------- Note: 'w' is for writing or replacing the entire contents of the file. For append, use 'a'. 2. How to read file in php? -------------------------------------------------------------------------------- < ?php $file = "test.txt"; $f_handle = fopen($file, "r"); while (!feof($f_handle)) { $line = fgets($f_handle); echo $line; } fclose($f_handle); ? > -------------------------------------------------------------------------------- An example of reading entire file: -------------------------------------------------------------------------------- < ?php $file = "test.txt"; $entire_file = file_get_contents($file); echo $entire_file; ? >
No comments:
Post a Comment