Read and write file on the hard disk with PHP

Read the file:

<? Php
$ File_name = “data.txt”;
$ File_pointer = fopen ($ file_name, “r”);
$ File_read = fread ($ file_pointer, filesize ($ file_name));
fclose ($ file_pointer);
print_r (“to read the contents of the file:”. $ file_read);
?>

Write file:

<? Php
$ File_name = “data.txt”;
$ File_pointer = fopen ($ file_name, “w”);
fwrite ($ file_pointer, “to write the content”);
print_r (“Data successfully written to the file.”);
?>

My program:

Read the file and put it in textarea. When some changes are made and submitted, the changes would be written in the file, and then read, displayed in the textarea. All is in one file named board.php.

board.php

<? Php
 $ Board = trim ($ _POST [“board”]);
 $ File_name = “board.php”;
 
 if ($ board! = null) (
   $ File_pointer = fopen ($ file_name, “w”);
   fwrite ($ file_pointer, $ board);
   print_r (“Data successfully written to the file.”);
 )

    $ File_pointer = fopen ($ file_name, “r”);
    $ File_read = fread ($ file_pointer, filesize ($ file_name));
    fclose ($ file_pointer);
?>
<Table>
  <Tr bgcolor = “# CDDADC” height = “30px;”>
    <Td align = “right”>
      <Form method = “post” action = “board.php”>
        <Textarea name = “board” cols = “80” rows = “15”> <? Php if ($ board == null) echo $ file_read; else echo $ board;?> </ Textarea>
        <Input type = “submit” value = “submit” />
      </ Form>
    </ Td>
  </ Tr>
</ Table>

Leave a Reply

Your email address will not be published. Required fields are marked *