PHP Tutorials – File manipulation (Part 1) !!

Started by ram, Jul 21, 2008, 08:13 PM

Previous topic - Next topic

ram

Hi [you],

PHP Tutorials – File manipulation (Part 1)

In the PHP Tutorial You will learn about File manipulation (Part 1) - Checking file existence, A file or directory, Determining file size, Creating and deleting files and Opening a file for writing, reading, or appending.

Checking file existence:

You can test for the existence of a file with the file_exists() function.
file_exists() takes one element, which is a string representing an absolute or relative path to a file that might or might not be there.
If the file is found, file_exists() returns true; otherwise, it returns false.

Example:

    if (file_exists("file.jpg")) {
    print "File exists";
    }


A file or directory:


You can confirm that the entity you're testing is a file, as opposed to a directory, with the is_file() function.
is_file() takes one argument, which is the file path and returns a Boolean value.

Example:

    if (is_file("file.jpg")) {
    print "file.jpg is a file";
    }


To check that the entity you're testing is a directory, you can do this with the is_dir(). is_dir() takes one argument, which is the directory path and returns a Boolean value.

Example:

    if (is_dir("/dir")) {
    print "/dir is a directory";
    }


Determining file size:

filesize() attempts to determine and return its size in bytes.
filesize() takes one argument, which is the file path. It returns false if it encounters problems.

Example:

    print "The size of file.jpg is.. ";
    print filesize("file.jpg");

Creating and deleting files:

If a file does not yet exist, you can create it with the touch() function.
touch() takes one argument, which is a string representing a file path
touch() attempts to create an empty file of that name.
If the file already exists, the contents aren't disturbed, but the modification date is updated to the time at which the function executed.

    touch("file.txt");

You can remove an existing file with the unlink() function.
unlink() accepts a file path:

    unlink("file.txt");

All functions that create, delete, read, write, and modify files on Unix systems require the correct file or directory permissions to be set.
Opening a file for writing, reading, or appending:

Before you can work with a file, you must first open it for reading, writing, or both, to do so use the fopen() function.
fopen() takes two argument, a string that contains the file path and a string containing the mode in which the file is to be opened.
The most common modes are read (r), write (w), and append (a). fopen() returns a file resource.

To open a file for reading, you use the following:

    $fp = fopen("file.text", 'r');

You use the following to open a file for writing:

    $fp = fopen("file.text", 'w');


To open a file for appending (that is, to add data to the end of a file), you use this:

    $fp = fopen("file.text", 'a');

fopen() returns false if the file cannot be opened for any reason.

It's a good idea to test the function's return value before proceeding to work with it. You can do so with an if statement:

    if ($fp = fopen("file.txt", 'w')) {
    // Execute this code
    }

Or you can use a logical operator to end execution if an essential file can't be opened:

    ($fp = fopen("file.txt", 'w')) or die ("Couldn't open file");

If the fopen() function returns true, the rest of the expression won't be parsed, and the die() function (which writes a message to the browser and ends the script) is never reached. Otherwise, the right side of the or operator is parsed and the die() function is called.

Assuming that all is well and you go on to work with your open file, you should remember to close it when you finish. You can do so by calling fclose()

fclose() takes one argument, which is the file resource returned from a successful fopen() call as its argument:


    fclose($fp);