php file operation explained

Solutions on MaxInterview for php file operation explained by the best coders in the world

showing results for - "php file operation explained"
Fynn
13 Apr 2019
1<html>
2
3   <head>
4      <title>Reading a file using PHP</title>
5   </head>
6   
7   <body>
8      
9      <?php
10         $filename = "tmp.txt";
11         $file = fopen( $filename, "r" );
12         
13         if( $file == false ) {
14            echo ( "Error in opening file" );
15            exit();
16         }
17         
18         $filesize = filesize( $filename );
19         $filetext = fread( $file, $filesize );
20         fclose( $file );
21         
22         echo ( "File size : $filesize bytes" );
23         echo ( "<pre>$filetext</pre>" );
24      ?>
25      
26   </body>
27</html>
Angela
04 Jan 2020
1<?php
2   $filename = "/home/user/guest/newfile.txt";
3   $file = fopen( $filename, "w" );
4   
5   if( $file == false ) {
6      echo ( "Error in opening new file" );
7      exit();
8   }
9   fwrite( $file, "This is  a simple test\n" );
10   fclose( $file );
11?>
12<html>
13   
14   <head>
15      <title>Writing a file using PHP</title>
16   </head>
17   
18   <body>
19      
20      <?php
21         $filename = "newfile.txt";
22         $file = fopen( $filename, "r" );
23         
24         if( $file == false ) {
25            echo ( "Error in opening file" );
26            exit();
27         }
28         
29         $filesize = filesize( $filename );
30         $filetext = fread( $file, $filesize );
31         
32         fclose( $file );
33         
34         echo ( "File size : $filesize bytes" );
35         echo ( "$filetext" );
36         echo("file name: $filename");
37      ?>
38      
39   </body>
40</html>