Basics
Creating a Document
Head and Body Elements
Formatting Text
Creating Lists
Embedding Images
URLs Explained
Linking to Documents
Creating Tables
Forms Form Basics
Input and Textarea
Select Lists
Frames Framesets
The Frame Element
Nested Framesets
Targetting Frames
Inline Frames
Tips and Tricks Meta Tags
Transition Effects
HTML Generators Create a Document
Create a List
Create a Table
HTML
Basics
Implementing CSS
CSS Syntax
Pseudo Classes/Elements
CSS Classes
CSS Properties Font Properties
Color and Background
Text Properties
Border Properties
Margins and Padding
Size and Position
Tips and Tricks Menu Buttons
Special Effects
CSS
Basics
Running a Script
Variables
Expressions and Operators
Objects.Properties.Methods
The Date Object
Strings
Regular Expressions
Defining RegExp Patterns
Branches and Conditions
Loops
Arrays Array Basics
Array Methods
Sorting Arrays

User-Defined Functions
Cookies
Windows
Frames
Tips and Tricks Image Replacement
Using Includes
Form Validation
Debugging
JavaScript
Basics
Creating a Script
Running a Script
Variables
Expressions and Operators
Strings Strings Basics
Strings and Substrings
Replacing Substrings
Regular Expressions
Branches and Conditions
Loops
Arrays Array Basics
Array Functions
Sorting Arrays
User-Defined Functions
Include and Require
Uploading Files
File Functions
Session Variables
Tips and Tricks Page Templates
Form Reply Scripts
Form Validation
JavaScript to PHP
PHP
Basics
Create and Drop
Show and Describe
Insert, Update and Delete
Querying
Join Queries
Functions
Table Locking
PHP/MySQL Functions Accessing a Database
Querying with PHP
Create and Drop with PHP
Insert and Update with PHP
Frequently Used Functions MySQL
Basics
Layout and Navigation
Page Content Style
Web Page Copy
Graphics and Animation
HTML Forms
Accessibility
Legal Requirements
MySQL

Home > PHP > File Functionsprinter version

PHP File Functions

PHP includes functions that allow files to be read from, written to and deleted.

The fopen() Function

Before any operations can be carried out on a file it must be opened. The fopen() function takes two arguments.



ModeDescription
'r'Open for read only and place the file pointer at the beginning of the file.
'r+'Open for read and write and place the file pointer at the beginning of the file.
'w'Open for write only, place the file pointer at the beginning of the file and truncate the file length to zero. If the file doesn't exist then attempt to create it.
'w+'Open for read and write, place the file pointer at the beginning of the file and truncate the file length to zero. If the file doesn't exist then attempt to create it.
'a'Open for write only and place the file pointer at the end of the file. If the file doesn't exist then attempt to create it.
'a+'Open for read and write and place the file pointer at the end of the file. If the file doesn't exist then attempt to create it.
'x'Create and open for write only and place the file pointer at the beginning of the file. If the file already exists then fopen() fails and returns FALSE. If the file doesn't exist then attempt to create it.
'x+'Create and open for read and write and place the file pointer at the beginning of the file. If the file already exists then fopen() fails and returns FALSE. If the file doesn't exist then attempt to create it.


Note: Opening a file for writing will fail if the appropriate file permissions have not been set for the file or the directory in which it's situated.


The fopen() function returns either ...



Example of Opening a File for Reading
<?php
    $handle = fopen ('my_file.txt', 'r') or exit ('Unable to open file');
?>


Closing a File

The fclose() function closes a file and only requires one argument - the file handle that was returned when the file was opened.

If a file is not closed then PHP will close it anyway when the script terminates, however, it's good programming practice to do so.

Example of Closing a File
<?php
    fclose ($handle);
?>


Reading a File

The fread() reads the contents of a file and takes two arguments.



If you want to read the whole file but don't know its size then the filesize() function can be used, which takes the file name as an argument.

Examples of Reading a File's Contents into a Variable
<?php
    $handle = fopen ('my_file.txt', 'r') or exit ('Unable to open file');
    // Read the first 10 characters of the file
    $contents = fread ($handle, 10);
    fclose ($handle);

    $handle = fopen ('my_file.txt', 'r') or exit ('Unable to open file');
    // Read the whole file
    $contents = fread ($handle, filesize ('my_file.txt');
    fclose ($handle);
?>


Writing to a File

The fwrite() function writes to a file and takes two arguments with an optional third argument.



If the 'w' mode is used when the file is opened then the file is overwritten. If the file doesn't already exist then it will be created.

If you wish to append the string to the file instead of overwriting the file then use the 'a' mode when opening the file.

Example of Writing to a File
<?php
    $handle = fopen ('my_file.txt', 'a') or exit ('Unable to open file');
    $append = ' the end.';
    fwrite ($handle, $append);
    fclose ($handle);
?>


Deleting a File

The unlink() function deletes a file and takes the path to the file as an argument.

Example of Deleting a File
<?php
    unlink ('documents/my_file.txt');
?>


Previous - PHP uploading files Previous - Uploading Files    Next - Session Variables Next - PHP session variables


Privacy | Terms | Contact | Links | Sitemap | RSS Feeds RSS and JavaScript Feeds
©2010 www.webdesignworkmate.co.uk all rights reserved 
Design and Production by smallbizonline website design © 2000-2010
Valid HTML 4.01! Level Double-A conformance icon, W3C-WAI Web Content Accessibility Guidelines 1.0Valid CSS!
PHPBasicsCreating a ScriptRunning a ScriptVariablesExpressions and OperatorsStringsStrings and SubstringsReplacing SubstringsRegular ExpressionsBranches and ConditionsLoopsArraysArray FunctionsSorting ArraysUser-Defined FunctionsInclude and RequireUploading FilesPHP file functionsFile FunctionsSession Variables
Recommended Reading
PHP and MySQL web development

programming PHP

PHP cookbook
Tips and TricksPage TemplatesForm Reply ScriptsForm ValidationJavaScript to PHP
Got any PHP Tips?
Send me your tip and if it's suitable I'll put it on the site, credit it to you and add a link back to your site.