Archive:PHP and MySQL Programming/File Handling: Difference between revisions

wikademia>Wikademia
 
(21 intermediate revisions by 7 users not shown)
Line 1: Line 1:
[[Programming:PHP and MySQL|TOC]]
== Introduction ==
== Introduction ==


As with most programming languages, filehandling is very important for storing and retrieving a wide variety of data.
As with most programming languages, filehandling is very important for storing and retrieving a wide variety of data.
 
{{ntnes}}
== Opening and Closing Files ==
== Opening and Closing Files ==
To open a file, we use the fopen() function. This function takes in two parameters. Firstly, a String containing the name of the file, and secondly, the mode with which to open the file with. It returns a filehandler. e.g. :
To open a file, we use the fopen() function. This function takes in two parameters. Firstly, a String containing the name of the file, and secondly, the mode with which to open the file with. It returns a filehandler. e.g. :


Line 31: Line 27:
  ?>
  ?>


In the above example, the PHP code will look within the current directory of the PHP file calling the include function, it will then open the objects.php file, and include its contentat of that file at the position of the include() function.
In the above example, the PHP code will look within the current directory of the PHP file calling the include function, it will then open the objects.php file, and include its content at of that file at the position of the include() function.


== Writing to a File ==
== Writing to a File ==
Line 70: Line 66:
   $total_contents = fread($f, filesize($filename));
   $total_contents = fread($f, filesize($filename));


[[../Syntax Overview/|Previous: Syntax Overview]] | [[../|TOC]] | [[../Database Connectivity/|Next: Database Connectivity]]


[[Programming:PHP and MySQL:PHP Syntax|<< 2. PHP Syntax Summary]] | [[Programming:PHP and MySQL|TOC]] | [[Programming:PHP and MySQL:Database Connectivity|>> 4. Database Connectivity]]
[[Category:PHP and MySQL Programming|{{SUBPAGENAME}}]]