Best Source of Web Development Tutorials, Articles, Programming Codes, Free PHP Scripts, PHP Tutorials and Much More
Login
Username:
Password:

Working with files in PHP

Working with files in PHP

By: programmersbank

Opening a File
The fopen function is used to open a file. This function needs two parameters. First, we must supply it with the name of the file that we want it to open. Secondly, we must tell the function what we plan on doing with that file (i.e. read from the file, write information, etc).

<?php
$handlefile= fopen("filename.txt", 'w');
?>

The file may be opened in one of the following modes:

r Read only. The file pointer is placed at the beginning of the file.
r+ Reading and writing. The file pointer is placed at the beginning of the file.
w Write only. The file pointer is placed at the beginning of the file, and the file contents are erased. If the file does not exist, an attempt will be made to create it.
w+ Reading and writing. The file pointer is placed at the beginning of the file, and the file contents are erased. If the file does not exist, an attempt will be made to create it.
a Write only. The file pointer is placed at the end of the file. If the file does not exist, an attempt will be made to create it.
a+ Reading and writing. The file pointer is placed at the end of the file. If the file does not exist, an attempt will be made to create it.

Closing a File
The fclose() function is used to close a file.
fclose($f);

Read data from file

The fread() function is used to read data from a file.

Delete file

In PHP, you can delete any file using unlink() function. unlink() function has one parameter that is file name you want to delete. The syntax of unlink() function is below:

<?
unlink($filename);
?>

Bookmark This Page