Breaking News
yeni film izle hd film seyret film izle

Understanding Files and Directories in PHP

Everything on your hard drive is stored as a file. There are ordinary program files, data files, files that are directories, and special files that help the hard drive keep track of the contents of folders and files.

PHP has functions that can work with any file type, but basically work with text files that contain data. A file is nothing more than an ordered sequence of bytes stored on a hard disk or other storage media.

Many differences exist between UNIX – based and Windows operating systems, one of them being the way directory paths are specified. UNIX – based systems such as Linux use slashes to delimit elements in a path, like this:

/home/work/PHP content/data.txt

Windows uses backslashes:


PHP on Windows automatically converts the former to the latter in most situations, so that you can safely use slashes in the script, instead of the operating system that the script is running on. Although, backslashes are necessary. Here, you need to use two backslashes in a row, because PHP interprets a backslash as escaping the following character:


Getting Information on Files

PHP provides some functions that allow you to access useful file information. As you can use

  • file_exists() to discover whether a file exists before attempting to open it.
  • file_exists( “/home/PHP Content/myfile.txt” )
  • File_exists() returns true if the file at the specified path exists, or false otherwise.

There are files related properties that can provide useful information. The available properties depend on the operating system in which the files are created and modified. On UNIX platforms such as Linux, properties include the time the file was last modified, the time it was last accessed, and the user permissions that have been set on the file.

PHP provides three time – related file functions:

    • fileatime() — It returns the time at which the file was last accessed as a UNIX timestamp. The file is considered accessed if its contents are read.


    • filectime() — It returns the time at which the file was last changed as a UNIX timestamp. The file is considered changed if it is created or written, or when its permissions have been changed.


  • filemtime() — It returns the time at which the file was last modified as a UNIX timestamp. The file is considered modified if it is created or has its contents changed.

A UNIX timestamp is an integer value indicating the number of seconds between the UNIX epoch (midnight on January 1, 1970) and the specified time and date.

The getdate() function is very useful when working with UNIX timestamps. It returns an associative array containing the date information present in a timestamp. The array includes such values as the year, the month, the day of the month, and so on. For example, you can set a variable such as $myDate to the value returned by getdate() , and then access the month component with $myDate[ “ month “ ] .

Retrieving a Filename from a Path

It is often very useful to be able to separate a filename from its directory path, and the basename()function does exactly that. It takes a complete file path and returns just the filename. For example, the following code assigns string.html to $filename:

$filename = basename( “/home/php/docs/string.html” );

You can also specify a directory path, in which case the rightmost directory name is returned. Here is an example that assigns the value docs to $dir:

$dir = basename( “/home/php/docs” );

The basename() Function retrieves the last whole string after the rightmost slash.

Check Also

Copying, Renaming, and Deleting Files

PHP also allows you to copy, rename, and delete files. The functions to perform these …