PHP Learn It! PHP Open File
"Best Deal"

"Bored - Do You Agree?"
t-shirtgirlspics

Bored - Studied Hard - Enjoy Yourself "TAKE A BREAK"

"Decide Now-So Many Choices"
"Amazing Deals"
Once You Know, You Newegg

Opening Files in PHP

In this section we are going to cover how to open local files and remote files/urls for reading and writing.

Open local files

Learn PHP and MySQL fast!

Learn how you can start building rich interactive database driven sites like a professional developer easily and quickly...

>> Order your copy now for just $39.95 <<

Just like how we create a new file, we also use fopen() to open existing files as well. However to open am existing file, we need to use different modes.

If you forgot what arguments fopen() takes, here they are again.

fopen ($filename, $mode);

PHP file open modes

Below is the list of modes to open existing files. We can use either of the modes to open files.

'r' Open for reading only; place the file pointer at the beginning of the file.
'r+' Open for reading and writing; place the file pointer at the beginning of the file.
'a' Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
'a+' Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it.

Let's look at some examples how to open files using fopen.

Example 1 - open file for reading

<?php
$fh = fopen("myfile.txt", "r");

if($fh==false)
	die("unable to create file");
?>

The code above opens myfile.txt for reading and places the pointer in the beginning of the files, which means, it will read everything from the start of the file to the end.

What happens if the file doesn't exist?

If the myfile.txt doesn't exist, the file handle $fh will return false. If you like to create the file in case it doesn't exist, you can use the file mode 'a'. This will create a new file myfile.txt, if it doesn't already exists.

Example 2 - open file for reading and writing

<?php
$fh = fopen(" myfile.txt", "r+");
if($fh==false)
	die("unable to create file");
?>

The code above opens myfile.txt for both reading and writing and places the pointer in the beginning of the file, which means, it will read or write from the start of the file.

What if I want to place pointer at the end of a file?

For that you can use file mode 'a' and 'a+' respectively to read/write from the end of the file.

Open remote files/urls

We can open files on local machine easily, like wise to open file sitting on a remote server or even urls, you can use fsockopen() function.

The function fsockopen() takes in several arguments as seen below.

fsockopen($hostname, $port, $errno, $errstr, $timeout);

Example 1 - open remote files

The example below shows how to read the source code of a page and display it.

<?php
$fp = fsockopen("www.somedomain.com", 80, $errno, $errstr, 30);
if ($fp == false) {
    echo "$errstr ($errno)";
} else {
   
	fputs($fp, "GET / HTTP/1.1\r\n");
	fputs($fp, "Host: www.somedomain.com\r\n");
	fputs($fp, "Connection: Close\r\n\r\n");

    while (!feof($fp)) {
        echo fgets($fp, 1024);
    }
    fclose($fp);
}
?>

So, in this tutorial we learned how to open files for reading and writing. In the next tutorial, we will cover how to read from files and how to write to files.

Build large scalable dynamic websites using PHP yourself
Learn PHP like a pro >>

<< PHP File Create Top PHP File Read >>