Star Hoster
Photoshop Tutorials
Anonymous Web Surfing
Funny Jokes
Myspace Friends
Open Treasure Chest
Building in Paradise


July 3, 2006

Working With PHP Files

Filed under: Intro PHP Tutorials — phpdeveloper @ 10:14 pm

Files

Working with files is a basic necessity for programmers and PHP gives you a great facility for creating, uploading, and editing files. This section is completely dedicated to how PHP can interact with files. After completing this section you should have a solid understanding of all types of file manipulation in PHP.

When you are manipulating files you must be very careful because you can do a lot of damage if you do something wrong.

Overview

The presentation of the file lessons will begin with how to create, open, and close a file. After establishing those basics, we will then cover other important file tasks, such as: read, write, append, truncate, and uploading files with PHP.

Create

Before you can do anything with a file it has to exist! In this section you will learn how to create a file using PHP.

In PHP, a file is created using a command that is also used to open files.
In PHP the fopen function is used to open files. However, it can also create a file if it does not find the file specified in the function call. So if you use fopen on a file that does not exist, it will create it, given that you open the file for writing or appending.

How to create a file?

The fopen function needs two important information to operate correctly. 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 for example : read from the file, write information etc.
As we want to create a file, we must supply a file name and tell PHP that we want to write to the file. Note: We have to tell PHP we are writing to the file, otherwise it will not create a new file.

Code:

$fname = “first.txt”;
$handle = fopen($fname, ‘w’) or die(”can’t open file”);
fclose($handle);

The file “first.txt” should be created in the same directory where this PHP code resides. PHP will see that “first.txt” does not exist and will create it after running this code. There’s a lot of information in those three lines of code, let’s make sure you understand it.

1. $fname = “first.txt”;

Here we create the name of our file, “first.txt” and store it into a variable $fname.

2. $handle = fopen($fname, ‘w’) or die(”can’t open file”);

This bit of code actually has two parts. First we use the function fopen and give it two arguments: our file name and we inform PHP that we want to write by passing the character “w”.

Second, the fopen function returns what is called a file handle, which will allow us to manipulate the file. We save the file handle into the $handle variable. We will talk more about file handles later on.

3. fclose($handle);

We close the file that was opened. fclose takes the file handle that is to be closed. We will talk more about this more in the file closing lesson.

PHP Permissions

If you are trying to get this program to run and you are having errors, you might want to check that you have granted your PHP file access to write information to the hard drive. Setting permissions is most often done with the use of an FTP program to execute a command called CHMOD. Use CHMOD to allow the PHP file to write to disk, thus allowing it to create a file.

FILE OPEN

In the previous lesson we used the function fopen to create a new file. In this lesson we will be going into the details of this important function.

Different Ways to Open a File

For many different technical reasons, PHP requires you to specify your intentions when you open a file. Below are the three basic ways to open a file and the corresponding character that PHP uses.

Read: ‘r’

Open a file for read only use. The file pointer begins at the front of the file.

Write: ‘w’

Open a file for write only use. In addition, the data in the file is erased and you will begin writing data at the beginning of the file. This is also called truncating a file. The file pointer begins at the start of the file.

Append: ‘a’

Open a file for write only use. However, the data in the file is preserved and you begin will writing data at the end of the file. The file pointer begins at the end of the file.

A file pointer is PHP’s way of remembering its location in a file. When you open a file for reading, the file pointer begins at the start of the file. This makes sense because you will usually be reading data from the front of the file.

However, when you open a file for appending, the file pointer is at the end of the file, as you most likely will be appending data at the end of the file. When you use reading or writing functions they begin at the location specified by the file pointer.

Explanation of Different Types of fopen

These three basic ways to open a file have distinct purposes. If you want to get information out of a file, then you would open the file for read only.
If you wanted to write a new file, or overwrite an existing file, then you would want to open the file with the “w” option. This would clean all existing data within the file.

If you wanted to add the latest information in your file, then you would want to open it to append the data on to the end. This would be the “a” option.

File Open: Advanced

There are additional ways to open a file. Above we stated the standard ways to open a file. However, you can open a file in such a way that reading and writing is allowable! This combination is done by placing a plus sign “+” after the file mode character.

Read/Write: ‘r+’

Opens a file so that it can be read from and written to. The file pointer is at the beginning of the file.

Write/Read: ‘w+’

This is exactly the same as r+, except that it deletes all information in the file when the file is opened.

Append: ‘a+’

This is exactly the same as r+, except that the file pointer is at the end of the file.

Example Code

$fname = “first.txt”;
$f = fopen($fname, ‘X’) or die(”Can’t open file”);
fclose($f);

You can use any option in place of ‘X’ i.e. ‘r’,’w’,’a’ etc.

Continue to part two of this tutorial

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

You must be logged in to post a comment.