Close Search Box
Search Box

Search: From:

Close
Newsletter

9Tutorials to your Inbox



Basic working with files in PHP

Basic working with files in PHP
Author lv1 (3100/5000)
2,492 views
1 Star2 Star3Star4 Star5 Star (4 votes, average: 3.75 out of 5)

Well, you want your script to open up a file? Edit it/rewrite it/do whatever? Okay… you can do that using PHP! It seems pretty basic I know, but you still need to master a few commands.

In this tutorial you will learn how to use the following commands/functions:
fopen() - Opens a file (for writing)
fclose() - Closes a file (no more writing)
fwrite() - Writes to a file
unlink() - Deletes a file

I am assuming you took the PHP Basics tutorial that I wrote so you at least have a basic understanding of some of the general commands we will be using, such as variables, etc.

In this tutorial there will be less explaining, and more coding. I will always comment the new parts of the code that you have not learned in my previous tutorials. In this tutorial that will be the f commands (open, close, write)

From here on, I will show you how to write, and read from files, mainly in examples you can learn from, and use in your own scripts.

Before We Start
Before we start, you need to be aware of the fopen modes. Different modes allow you to do different things with files. Eg; Mode “w+” opens a file for writing, it deletes the current file and if it does not exist attempts to create it… it writes from the start of the file.

Modes
Mode “r” - Opens a file for reading only
Mode “r+” - Opens a file for reading and writing, writes from the start of the file (does not delete previous data)
Mode “w” - Opens a file for writing only, writes from the start of the file and deletes any previous data, if the file does not exist, php will attempt to create it.
Mode “w+” - Opens a file for reading and writing, writes from the start of the file and deletes any previous data, if the file does not exist, php will attempt to create it.
Mode “a” - Open a file for writing only, start writing at the end of the file, if the file does not exist, php will attempt to make it.
Mode “a+” - Open a file for reading and writing, start writing at the end of the file, if the file does not exist, php will attempt to make it.

Example #1 - Opening and Writing to a File (In “w” mode)
NOTE: The directory on your server must be writeable before you can execute these scripts… linux chmod 0777 (777)

Example #2 - Reading data from a file (with “r” mode)
NOTE: The file you define (in our case file.txt) must exist with data in it! Or else this script will fail! Also note that when using fread the second arguement in it tells it how long to read for… we want it to read the full file so we use filesize($filename) to get its full length…

Example #3 - Deleteing a file (With the unlink function)

I hope this tutorial helped you learn how to work with files.

Copyright @ Justin 2007

del.icio.us:Basic working with files in PHP digg:Basic working with files in PHP spurl:Basic working with files in PHP newsvine:Basic working with files in PHP blinklist:Basic working with files in PHP furl:Basic working with files in PHP reddit:Basic working with files in PHP blogmarks:Basic working with files in PHP Y!:Basic working with files in PHP magnolia:Basic working with files in PHP segnalo:Basic working with files in PHP

Post a Comment »








Safari hates me

Comment Guidelines

  • Hyperlinks are automatically generated.
  • <em>italic</em>
  • <strong>bold</strong>
  1. mzeid October 23, 2007

    A Million thanks for your share. Can you please post the link to your PHP Basic Tutorial. There is no link to it in your post.

    Thanks,
    Mohamed

  2. pramod August 4, 2008

    ho w r u