PHP and file handling

PHP can read and write to files on the server. That is one way to get lots of data into your Web pages or to save it after users input it. Remember that PHP is executed by the Web server so it cannot access files on the user's computer.

Read

Create an empty text file called names.txt and paste the names in:

George
John
Paul
Ringo
        

Make sure that there is a carriage return (line break) between each name as that is how PHP splits the array elements

Upload it to the server in the same location as your PHP pages. If you are using ftp to a proper server make sure that the owner is www-data (Apache) or the file permissions are read and write for everyone. Also create a new PHP page called files.php. Paste in this code:

<?php
    $names = file("./names.txt");
    echo "<pre>";    
    print_r($names);
    echo "</pre>";
?>
        

The first line reads the content of the file into a variable. The dot (.) is a quick way of saying that the file is in the current directory/folder (the same place as the PHP page). If the file was in a sub-directory you could say that:

$names = file("./files/names.txt");
        

You can also refer to a parent directory using two dots (..) or even give the file location precisely including the server:

$names = file("http://www.yourwebskills.com/names.txt");
        

The rest of the code is not new to you.

To improve appearance you could now copy in the FOREACH loop from loops.php and echo the names to the Web page properly. Add more names and try again. Try adding a new name on the same line as one of the existing ones and see what happens.

Write

To write to a file you need to open it with write access (this locks the file to stop anyone else writing to it at the same time). Then append (add) data:

$handle = fopen("./names.txt", "a");
fwrite($handle, "Pete");
fclose($handle);
        

The first line of code sets up a connection to the file and locks it for access. No one else can write to that file now. The "a" parameter opens the file ready to append data or creates the file if it does not exist.

The second line of code writes to the file. The file is identified by $handle which is like a signpost or link to it. The text to write is after the comma and in quotes (you could use a variable there instead).

Closing the file unlocks it so it can be accessed next time.

Create a new PHP file and paste the above code in (call it filewrite.php). Load the page once (it should be blank as it has no HTML or ECHO statements). Now edit the code to change the name which will be added. Upload the page again and view it in a browser. Again the page should be blank.

Having loaded the new page twice there should be two extra entries in your names.txt file. You can download that file and open it or use your own page (files.php) in a browser to see the content. There is a problem. There are no carriage returns between the new entries. The next page fixes that.

Listing files

If you do not know the name of the file you can list the contents of the directory into an array:

$filelist=scandir("/uploads");
        

and then loop through them looking for the type of file you want.

submit to reddit Delicious Tweet