Skip Navigation Links
EZWEB
PHPExpand PHP

PHP File Example

Writing to a file

Code Example:

1

<html>

2

   <head>

3

      <title>Write File</title>

4

   </head>

5

   <body>

6

      <?php

7

         $filename ="file_name.txt";

8

         $file=fopen($filename,"w");

9

         fwrite($file,"This is a test\n");

10

         fclose($file);

11

      ?>

12

   </body>

13

</html>

Save as: Write.php

Explanation

1

HTML code

2

HTML code

3

HTML code

4

HTML code

5

HTML code

6

Start PHP script

7

Create a variable names $filename to hold the entire path to the file to write to

8

Open the filename specified in the variable $filename for writing and assign it to the file handler $file

9

Write to the file “This is a test” and go to the next line

10

Close the file

11

End PHP script

12

HTML code

13

HTML code


Reading from a file

Code Example:

1

<html>

2

   <head>

3

      <title>Read File</title>

4

   </head>

5

   <body>

6

      <?php

7

         $filename ="file_name.txt";

8

        @ $file = fopen( $filename, "r" )or
        die ( "Cannot open the file" );

9

         if ( filesize($filename) != 0 ){

10

            $filesize=filesize($filename);

11

            $text=fread($file,$filesize);

12

            echo("$text");

13

          }

14

           fclose($file);

15

          ?>

16

   </body>

17

</html>

Save as: Read.php

Explanation

1

HTML code

2

HTML code

3

HTML code

4

HTML code

5

HTML code

6

Start PHP script

7

Create a variable names $filename to hold the entire path to the file to write to

8

Open the filename specified in the variable $filename for reading and assign it to the file handler $file, if it cannot be opened then display an error message

9

If the filesize of the file is not 0 then

10

Retrieve the size of the file and assign it to the variable $filesize

11

Using the filehandler and the filesize read from the file and store it in the variable $text

12

Display the contents of the file

13

Close the IF statement

14

Close the file

15

End the PHP script

16

HTML code

17

HTML code


Change the “w” to “a”
This will append the data instead of rewriting it

Creating a comma delimited file
First create the HTML file with a form

Code Example:

1

<html>

2

   <head>

3

      <title>Write File</title>

4

   </head>

5

   <body>

6

  <form  method="post" action="writeToFile.php">

7

   Name:<input name="myName" type="text"><br />

8

   EMail:<input name="myEmail" type="text"><br />

9

   <input type="submit">

10

  </form>

11

   </body>

12

</html>

Save as: AddData.html

Now create the PHP page

Code Example:

1

<html>

2

   <head>

3

      <title>Write File</title>

4

   </head>

5

   <body>

6

  <?php

7

   $name=$_POST['myName'];

8

   $email=$_POST['myEmail'];

9

   $filename ="testFile.txt";

10

  $file=fopen($filename,"w");

11

   fwrite($file,"$name,$email \n");

12

   fclose($file);

13

   ?>

14

   </body>

15

</html>

Save as: writeToFile.php

Test it
Now change the “w” to “a” and now it appends to the file
Enter in some data

Explanation

1

HTML code

2

HTML code

3

HTML code

4

HTML code

5

HTML code

6

Start PHP script

7

Create a variable called $name to store the data from the form in the myName element

8

Create a variable called $email to store the data from the form in the myEmail element

9

Create a variable named $filename to store the name of the file

10

Open the file for writing

11

Write a line to the file with the data separated by commas

12

Close the file

13

End the PHP script

14

HTML code

15

HTML code


Reading the comma delimited file

Code Example:

1

<html>

2

   <head>

3

      <title>Write File</title>

4

   </head>

5

   <body>

6

  <?

7

   $fp = fopen ("testfile.txt","r");

8

   while ($data = fgetcsv ($fp, 1000, ",")) {

9

   $num = count ($data);

10

  for ($c=0; $c<$num; $c++) {

11

   echo "$data[$c]    ";

12

   }

 

    echo"<br />";

 

  }

 

  fclose ($fp);

13

   ?>

14

   </body>

15

</html>

Save as: ViewFile.php

fgetcsv
Gets line from file pointer and parse for CSV fields

Description

array fgetcsv (int fp, int length, string [delimiter])
fgetcsv() parses the line it reads for fields in CSV format and returns an array containing the fields read. The field delimiter is a comma, unless you specify another delimiter with the optional third parameter.
Fp must be a valid file pointer to a file successfully opened by fopen(), popen(), or fsockopen()
Length must be greater than the longest line to be found in the CSV file (allowing for trailing line-end characters).
fgetcsv() returns FALSE on error, including end of file.





Free Tutorials and Training