Working with Files in PHP

In this tutorial, we look at using files in PHP. In the first part of this article, we are going to look at how to get a directory listing of all the files and directories at a specified path on your web server.

We then move on to looking at how to write text files on the server, in csv format so they can easily be used in the many applications that can read csv (Comma Seperated Value) files, and how to dump the contents of a Dreamweaver MX recordset to a csv file.

Next we look at reading back existing csv files, and storing the data in an array so that it's easy to work with it. Finally we close the article by looking at some other file related commands, such as using unlink() to delete a file.

$2.89
- OR -

Overview

Table of Content:

  • Why Read and Write files from your Web Site?
  • Reading the files in a Directory
  • PHP Commands for working with Directories
  • Working Example - Reading the Files in a Directory
  • Exanding the Example Code
  • Writing files from a Web Page
  • Writing Files - The Basics
  • A Simple Example
  • Dumping the contents of a Database Table to a File
  • Reading Data from a Text File
  • A Working Example
  • Populating a Drop Down menu with the data read from the File
  • Other File Commands
  • Checking a File exists
  • Deleting a File
  • Overview

Gareth Downes-Powell

Gareth Downes-PowellGareth has a range of skills, covering many computer and internet related subjects. He is proficient in many different languages including ASP and PHP, and is responsible for the setup and maintenance of both Windows and Linux servers on a daily basis.


In his daily web development work he uses the complete range of Macromedia software, including Dreamweaver MX, Flash MX, Fireworks MX and Director to build a number of websites and applications. Gareth has a close relationship with Macromedia, and as a member of Team Macromedia Dreamweaver, he has worked closely in the development of Dreamweaver, and was a beta tester for Dreamweaver MX.


On a daily basis he provides support for users in the Macromedia forums, answering questions and providing help on a range of different web related subjects. He has also written a number of free and commercial extensions for Dreamweaver MX, to further extend its capabilities using its native JavaScript API’s or C++.


As a web host, Gareth has worked with a range of different servers and operating systems, with the Linux OS as his personal favourite. Most of his development work is done using a combination of Linux, Apache and MySQL and he has written extensively about setting up this type of system, and also running Apache and MySQL under Windows.

See All Postings From Gareth Downes-Powell >>

Reviews

Getting always the right path...

February 28, 2004 by Kutt Niinepuu
Instead of looking up your server path via the phpinfo(); function, you could add the following code in place of the path declaration. As an added bonus, you can freely move the file afterwrds, as the path is updated automatically...

$path = dirname(__FILE__);

if (eregi("WIN",PHP_OS)) {
$path = str_replace("\\","/",$path);
};
$path .= "/your_own_subdirectories_here/filename_if you_need_it.txt";

(you need to point the filename if you are writing to a file)

You must me logged in to write a review.