Tuesday 30, September 2014
Welcome Guest, Register | Login  
      Home    |    Tutorials    |    Free Ebooks    |    Free Scripts    |    Articles    |    Blog     |    About Us    |    Contact Us

Files

PHP is a very useful language for working with files. Although it may not be as robust as other languages, such as PERL, when it comes to parsing files, PHP still provides great power and flexibility when it comes to working with text files.

 

Opening a File:

Before you can use PHP to read a file, you must first open it. To open a file, you use the fopen() function.

$file_handler = fopen($file, $mode)

You must assign the function to a variable, as above. This is referred to as a file pointer. You use the file pointer to reference the open file throughout your script.

The fopen() function takes two arguments:

  • $file is the name (and path, if required) of the file.
  • $mode is one of a list of available modes:

1.        r— Open the file as read-only. You cannot write anything to the file. Reading begins at the beginning of the file.

2.        r+ — Open the file for reading and writing. Reading and writing begin at the beginning of the file. You will delete existing contents if you write to the file without first moving the internal pointer to the end of the file.

3.        w— Open the file for write-only. You cannot read data from the file. Writing begins at the beginning of the file. You will again delete contents if you write to the file without first moving the pointer to the end of the file. If the file specified by the $fie argument does not exist, then PHP attempts to create the file. Make sure your permissions are set correctly!

4.        w+ — As above, but the file may also be read.

5.        a— This mode is the same as the 'w' mode, with the exception that the internal pointer is placed at the end of the file. Existing contents will not be overwritten unless you rewind the internal pointer to the beginning of the file.

6.        a+ — As above, but the file may also be read.

Additionally, if the file is a binary file, you must include a "b" in the mode if your OS is Windows. This chapter only deals with ASCII text files, so you will not require the "b: mode in the following example. If you do need to open a binary file, an example is below:

$file_handle = fopen($filename, "rb+")

Note the differences between the "w" and "a" modes. Using "w" to open a file effectively deletes the contents of the file, while using the "a" mode retains the contents of the file and allows you to append additional data to the end of the file.

Once you have the file opened, you can then read from or write to the file.

 

Reading a File:

There are multiple functions available to read from a file.

You can read data from the file using the fread() function:

$file_contents = fread($file_pointer, $length_to_read);

You must assign the value of fread() to a variable, which will contain the data that has been read. The fread() function takes two arguments:

1.      $file_pointer is the file pointer to which you assigned the value from the fopen() function.

2.      $length is the amount of bytes that you wish to read.

When using fread() to read a file, you can read in the entire file. To do this, you need to know the byte-size of the file. You can easily find the total byte-size of a file using the filesize() function:

 

$bytes = filesize($filename);

 

The following script demonstrates how to open a short text file and display its contents:

 

$file = "/path/to/text.txt";

$filepointer = fopen($file, "r");

$contents = fread($filepointer, filesize($file));

echo "

" . htmlentities($contents) . "
";

 

Closing a File:

When you have finished reading from or writing to a file that has been opened using the fopen() function, you should always close it using the fclose() function:

 

fclose($filepointer);

Closing the file allows you to clean up any memory that was used to open it and also prevents possible file corruption that sometimes (rarely) occurs when a file is left open.

 

 
     
   
PHP Tutorial
 
  Introduction
Introduction
  Variables
Variables
  Strings
Strings
  Operators
Operators
  If Else Statement
If Else Statement
  Loops
Loops
  Functions
Functions
  Forms
Forms
  Sessions
Sessions
  Cookies
Cookies
  Files
Files
  E-mail
E-mail
  Error Handling
Error Handling
  File Upload
File Upload
  PHP Databases
PHP Databases
  Connecting to a MySQL Database
Connecting to a MySQL Database
  MySQL Data Types
MySQL Data Types
  Inserting Data Into a Table
Inserting Data Into a Table
  Select Statement
Select Statement
  Where Clause
Where Clause
  Order By
Order By
  Update Statement
Update Statement
  Delete From Statement
Delete From Statement
 
 
 
Web Designing Tutorials
  HTML Tutorial
HTML Tutorial
  DHTML Tutorial
DHTML  Tutorial
  CSS Tutorial
CSS Tutorial
  XHTML Tutorial
XHTML Tutorial
 
Programming Languages Tutorials
  C Language Tutorial
C Language Tutorial
  C++ Tutorial
C++ Tutorial
  Java Language Tutorial
Java Language Tutorial
  Data Structure Theory Tutorial
Data Structure Theory Tutorial
 
Server Side Scripting Tutorials
  PHP Tutorial
PHP Tutorial
  SQL Tutorial
SQL Tutorial
  ASP Tutorial
ASP Tutorial
 
Client Side Scripting Tutorials
  JavaScript Tutorial
JavaScript Tutorial
  VBScript Tutorial
VBScript Tutorial
 
 
 
POPULAR E-BOOKS
 
Download How to Launch Your Music Career in 21 Days Ebook How to Launch Your Music Career in 21 Days
   
Download Marathon List Building Ebook Marathon List Building
   
Download 	A Beginners C++ Ebook A Beginners C++
   
Download SEO  Search Engine Optimization Made Easy Ebook SEO Search Engine Optimization Made Easy
   
Download The Top 10 Ways to Speed Up Your Windows PC Right Now  For Free Ebook The Top 10 Ways to Speed Up Your Windows PC Right Now For Free
   
     
Studiesinn.com 2014 All Rights Reserved.
 
Website Designed & Developed by TechXprtz