File access with php

Working with Files in PHP

You may well be familiar with databases such as MySQL and Access which are an ever-increasingly common means of storing data. But data is also stored in files, like Word documents, event logs, spreadsheets, image files, and so on. Databases generally require a special query language to retrieve information, whereas files are ‘flat’ and usually appear as a stream of text.

Most often when working with files you’ll read from or write to them. When you want to read the contents of a file you first have to open it, then read as much of the contents as you want, then close the file when you’re finished. When writing to a file, it also needs to be opened (or perhaps created if it doesn’t already exist), then you write your data to it, and close the file when you have finished. In PHP5 there are some built-in wrapper functions that handle the opening and closing automatically for you, but it still happens under the hood.

You may also find it useful to find out more about the file by examining its attributes before you start working with it. For example, does the file exist? When was it last updated? When was it created?

PHP provides a range of functions which allow you to work with files, and in this article I’ll demonstrate some of them for you.

File Attributes

File attributes are the properties of a file, for example its size, the last time it was accessed, its owner, etc. Let’s look at how you find out more about the files you’re working with.

File Size

The function filesize() retrieves the size of the file in bytes.

When executed, the example code displays:

C:Windowswin.ini is 510 bytes.

The use of a file on a Windows system here highlights an important point; because the backslash has special meaning as an escape character in strings, you’ll need to escape it by adding another backslash.

If the file doesn’t exist, the filesize() function will return false and emit an E_WARNING , so it’s better to check first whether the file exists using the function file_exists() :

In fact many of the functions presented in this section have the same behavior, i.e., emit warnings. I’ve not included a check with file_exists() in the rest of my examples for the sake of brevity, but you’ll want to use it when you write your own code.

Читайте также:  Примеры форм html javascript

File History

To determine when a file was last accessed, modified, or changed, you can use the following functions respectively: fileatime() , filemtime() , and filectime() .

"; echo $f . " was modified on " . date($dateFormat, $mtime) . ".
"; echo $f . " was changed on " . date($dateFormat, $ctime) . ".";

The code here retrieves the timestamp of the last access, modify, and change dates and displays them,

C:Windowswin.ini was accessed on Tue 14 Jul 2009 4:34 AM. C:Windowswin.ini was modified on Fri 08 Jul 2011 2:03 PM. C:Windowswin.ini was changed on Tue 14 Jul 2009 4:34 AM.

To clarify, filemtime() returns the time when the contents of the file was last modified, and filectime() returns the time when information associated with the file, such as access permissions or file ownership, was changed.

The date() function was used to format the Unix timestamp returned by the file*time() functions. Refer to the documentation for the date() function for more formatting options.

File Permissions

Before working with a file you may want to check whether it is readable or writeable to the process. For this you’ll use the functions is_readable() and is_writeable() :

"; echo $f . (is_writable($f) ? " is" : " is not") . " writeable.";

Both functions return a Boolean value whether the operation can be performed on the file. Using the ternary operator you can tailor the display to state whether the file is or is not accessible as appropriate.

C:Windowswin.ini is readable. C:Windowswin.ini is not writeable.

File or Not?

To make absolutely sure that you’re dealing with a file you can use the is_file() function. is_dir() is the counterpart to check if it is a directory.

"; echo $f . (is_dir($f) ? " is" : " is not") . " a directory.";
C:Windowswin.ini is a file. C:Windowswin.ini is not a directory.

Reading Files

The previous section showed how you can find out a good deal about the files you’re working with before you start reading or writing to them. Now let’s look at how you can read the contents of a file.

The convenience function file_get_contents() will read the entire contents of a file into a variable without the need to open or close the file yourself. This is handy when the file is relatively small, as you wouldn’t want to read in 1GB of data into memory all at once!

For larger files, or just depending on the needs of your script, it’s may be wiser to handle the details yourself. This is because once a file is opened you can seek to a specific offset within it and read as little or as much data as you want at a time.

Читайте также:  Все для начинающего вебмастера

The function fopen() is used to open the file:

Using the function fopen() , two arguments are needed – the file I want to open and the mode, which in this case is “r” for read. The function returns a handle or stream to the file, stored in the variable $f1 , which you use in all subsequent commands when working with the file.

The most common mode values are:

table of basic file modes

For other values, refer to the list in PHP’s fopen() page.

To read from the opened file one line at a time, use the function fgets() :

Using a do – while loop is a good option because you may not know in advance how many lines are in the file. The function feof() checks whether the file has reached its end – the loop continues until the end of file condition is reached.

To tidy up after I’ve finished reading the file, the function fclose() is used to close the file.

Writing Files

Two commonly used modes when writing to a file using the function fwrite() are “w” and “a” – “w” indicates you want to write to the file but it will remove any of the existing file contents beforehand, while “a” means that you will append any new data to what already exists in the file. You need to be sure you are using the correct option!

In this example I’ll use “a” to append:

First the file name is assigned to a variable, then the file is opened in mode “a” for append. The data to be written is assigned to a variable, $output , and fwrite() adds the data to the file. The process is repeated to add another line, then the file is closed using fclose() . The pre-defined constant PHP_EOL adds the newline character specific to the platform PHP is running on.

The file contents after executing the above code should look like this:

The convenience function file_put_contents() can also write to a file. It accepts the file name, the data to be written to the file, and the constant FILE_APPEND if it should append the data (it will overwrite the file’s contents by default).

Here’s the same example as above, but this time using file_put_contents() :

Working with CSV Files

CSV stands for comma separated variable and indicates the file contains data delimited with a comma. Each line is a record, and each record is made up of fields, very much like a spreadsheet. In fact, software such as Excel provides the means to save files in this format. Here is an example as displayed in Excel 2007 (which doesn’t show the commas):

Читайте также:  Set session data in php

Excel showing a CSV file

There are 4 lines of data, or records, in the file containing first name, last name, age and job title. To read the file and extract the individual fields, start by opening the file in read mode:

Now I need to use a function specifically to read the CSV formatted file, fgetcsv() ; a while loop is used to cater for the fact that the number of records is expected to vary:

This will produce output with a field on each line:

Fred Smith 45 Engineer Susan Brown 33 Programmer Ed Williams 24 Trainee Jo Edwards 29 Analyst

Let’s examine the loop. First, fgetcsv() has one argument only – the file handle; the comma delimiter is assumed. fgetcsv() returns data in an array which is then processed using a foreach loop. When all records have been read the file is closed using fclose() .

Let’s now look at how you can update or write to a CSV file, this time using the function fputcsv() . Because I am using the same file that I used above, I will open it in append mode, and the data to be added to the file has been defined in an array.

The contents of the file as displayed in Excel now look like this:

Excel showing a CSV file

Summary

This article has given you an introduction into working with files and shown how you can find information about files, read from and write to files. There are many other functions you can use when working with files – this article has covered some of the most commonly used. You’ll find more information on PHP’s file system functions page.

Share This Article

Iain Tench has worked in the IT industry for 30 years as a programmer, project manager (Prince2 Practitioner), consultant, and teacher. As a project manager, he specialized in integration projects mainly involving payment systems in the financial sector. He now teaches and has acquired a Masters degree in Internet Systems Development as well as a teaching qualification. Iain’s specialized areas of teaching is web technologies, mainly programming in a variety of languages, HTML, CSS and database integration. He’s also written a book about Dreamweaver CS3 for Hodder Education.

Источник

Оцените статью