0% found this document useful (0 votes)
23 views10 pages

Files in PHP

The document discusses different ways to open files in PHP using specific characters: 'r' for read-only, 'w' for write and erase existing data, 'a' for write and append to existing data, 'r+' for read and write, 'w+' for write and erase like 'w' but also read, and 'a+' for write and append like 'a' but also read. It also provides code examples for creating, opening, reading, writing, appending, closing, and deleting files in PHP.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODP, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views10 pages

Files in PHP

The document discusses different ways to open files in PHP using specific characters: 'r' for read-only, 'w' for write and erase existing data, 'a' for write and append to existing data, 'r+' for read and write, 'w+' for write and erase like 'w' but also read, and 'a+' for write and append like 'a' but also read. It also provides code examples for creating, opening, reading, writing, appending, closing, and deleting files in PHP.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODP, PDF, TXT or read online on Scribd
You are on page 1/ 10

PHP 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. 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.

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.

Create a File

$my_file = 'file.txt'; $handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file);

Open a File

$my_file = 'file.txt'; $handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file); //open file for writing ('w','r','a').

Read a File

$my_file = 'file.txt'; $handle = fopen($my_file, 'r'); $data = fread($handle,filesize($my_file));

Write to a file

$my_file = 'file.txt'; $handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file); $data = 'This is the data'; fwrite($handle, $data);

Append to a File

$my_file = 'file.txt'; $handle = fopen($my_file, 'a') or die('Cannot open file: '.$my_file); $data = 'New data line 1'; fwrite($handle, $data); $new_data = "\n".'New data line 2'; fwrite($handle, $new_data);

Close a file

$my_file = 'file.txt'; $handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file); //write some data here fclose($handle);

Delete a file

$my_file = 'file.txt'; unlink($my_file);

You might also like