Filesystem Functions

Filesystem Functions

basename

Name

basename — return filename component of path

Description

string basename(string path);

chgrp

Given a string containing a path to a file, this function will return the base name of the file.

On Windows, both slash (/) and backslash (\) are used as path separator character. In other environments, it is the forward slash (/).

Example 1. basename example

$path = "/home/httpd/html/index.php3";

$file = basename($path); // $file is set to "index.php3"

See also: dirname

Name

chgrp — change file group

Description

int chgrp(string filename, mixed group);

Attempts to change the group of the file filename to group. Only the superuser may change the group of a file arbitrarily; other users may change the group of a file to any group of which that user is a member.

Returns true on success; otherwise returns false. On Windows, does nothing and returns true.

See also chown and chmod.

chmod

Name

chmod — change file mode

Description

int chmod(string filename, int mode);

Attempts to change the mode of the file specified by filename to that given in mode.

Note that mode is not automatically assumed to be an octal value. To ensure the expected operation, you need to prefix mode with a zero (0):

chmod( "/somedir/somefile", 755 ); // decimal; probably incorrect chmod( "/somedir/somefile", 0755 ); // octal; correct value of mode

Returns true on success and false otherwise. See also chown and chgrp.

chown

Name

chown — change file owner

Description

int chown(string filename, mixed user);

Attempts to change the owner of the file filename to user user. Only the superuser may change the owner of a file.

Returns true on success; otherwise returns false.

On Windows, does nothing and returns true.

See also chown and chmod.

clearstatcache

Name

clearstatcache — clear file stat cache

Description

void clearstatcache(void);

copy

Invoking the stat() or lstat() system call on most systems is quite expensive. Therefore, the result of the last call to any of the status functions (listed below) is stored for use on the next such call using the same filename. If you wish to force a new status check, for instance if the file is being checked many times and may change or disappear, use this function to clear the results of the last call from memory.

Affected functions include stat, lstat, file_exists, is_writeable, is_readable, is_executable, is_file, is_dir, is_link, filectime, fileatime, filemtime, fileinode, filegroup, fileowner, filesize, filetype, and fileperms.

Name

copy — copy file

Description

int copy(string source, string dest);

Makes a copy of a file. Returns true if the copy succeeded, false otherwise.

Example 1. copy example

if (!copy($file, $file.'.bak')) { print("failed to copy $file...<br>\n");

}

See also: rename

dirname

Name

dirname — return directory name component of path

Description

string dirname(string path);

Given a string containing a path to a file, this function will return the name of the directory.

On Windows, both slash (/) and backslash (\) are used as path separator character. In other environments, it is the forward slash (/).

Example 1. dirname example

$path = "/etc/passwd";

$file = dirname($path); // $file is set to "/etc"

See also: basename

fclose

Name

fclose — close an open file pointer

Description

int fclose(int fp);

The file pointed to by fp is closed.

Returns true on success and false on failure.

The file pointer must be valid, and must point to a file successfully opened by fopen or fsockopen.

feof

fgetc

Name

feof — test for end-of-file on a file pointer

Description

int feof(int fp);

Returns true if the file pointer is at EOF or an error occurs; otherwise returns false.

The file pointer must be valid, and must point to a file successfully opened by fopen, popen, or

fsockopen.

Name

fgetc — get character from file pointer

Description

string fgetc(int fp);

Returns a string containing a single character read from the file pointed to by fp. Returns FALSE on EOF (as does feof).

The file pointer must be valid, and must point to a file successfully opened by fopen, popen, or

fsockopen.

See also fopen, popen, fsockopen, and fgets.

fgets

Name

fgets — get line from file pointer

Description

string fgets(int fp, int length);

Returns a string of up to length - 1 bytes read from the file pointed to by fp. Reading ends when length - 1 bytes have been read, on a newline, or on EOF (whichever comes first).

If an error occurs, returns false.

The file pointer must be valid, and must point to a file successfully opened by fopen, popen, or

fsockopen.

See also fopen, popen, fgetc, and fsockopen.

fgetss

Name

fgetss — get line from file pointer and strip HTML tags

Description

file

string fgetss(int fp, int length);

Identical to fgets, except that fgetss attempts to strip any HTML and PHP tags from the text it reads. See also fgets, fopen, fsockopen, and popen.

Name

file — read entire file into an array

Description

array file(string filename);

Identical to readfile, except that file() returns the file in an array. See also readfile, fopen, and popen.

file_exists

Name

file_exists — Check whether a file exists.

Description

int file_exists(string filename);

Returns true if the file specified by filename exists; false otherwise. See also clearstatcache.

fileatime

Name

fileatime — get last access time of file

Description

int fileatime(string filename);

Returns the time the file was last accessed, or false in case of an error.

filectime

Name

filectime — get inode modification time of file

Description

int filectime(string filename);

Returns the time the file was last changed, or false in case of an error.

filegroup

Name

filegroup — get file group

Description

int filegroup(string filename);

Returns the group ID of the owner of the file, or false in case of an error.

fileinode

Name

fileinode — get file inode

Description

int fileinode(string filename);

Returns the inode number of the file, or false in case of an error.

filemtime

Name

filemtime — get file modification time

Description

int filemtime(string filename);

Returns the time the file was last modified, or false in case of an error.

fileowner

Name

fileowner — get file owner

Description

int fileowner(string filename);

Returns the user ID of the owner of the file, or false in case of an error.

fileperms

Name

fileperms — get file permissions

Description

int fileperms(string filename);

Returns the permissions on the file, or false in case of an error.

filesize

Name

filesize — get file size

Description

int filesize(string filename);

Returns the size of the file, or false in case of an error.

filetype

Name

filetype — get file type

Description

string filetype(string filename);

Returns the type of the file. Possible values are fifo, char, dir, block, link, file, and unknown. Returns false if an error occurs.

fopen

Name

fopen — open file or URL

Description

int fopen(string filename, string mode);

If filename begins with "http://" (not case sensitive), an HTTP 1.0 connection is opened to the specified server and a file pointer is returned to the beginning of the text of the response.

Does not handle HTTP redirects, so you must include trailing slashes on directories.

If filename begins with "ftp://" (not case sensitive), an ftp connection to the specified server is opened and a pointer to the requested file is returned. If the server does not support passive mode ftp, this will fail.

If filename begins with anything else, the file will be opened from the filesystem, and a file pointer to the file opened is returned.

If the open fails, the function returns false.

mode may be any of the following:

  • 'r' - Open for reading only; place the file pointer at the beginning of the file.

  • 'r+' - Open for reading and writing; place the file pointer at the beginning of the file.

  • 'w' - Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

  • 'w+' - Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

  • 'a' - Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.

  • 'a+' - Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it.

As well, mode may contain the letter 'b'. This is useful only on systems which differentiate between binary and text files (i.e., it's useless on Unix). If not needed, this will be ignored.

Example 1. fopen() example

$fp = fopen( "/home/rasmus/file.txt", "r" );

$fp = fopen( "http://www.php.net/", "r" );

If you are experiencing problems with reading and writing to files and you're using the server module version of PHP, remember to make sure that the files and directories you're using are accessible to the server process.

See also fclose, fsockopen, and popen.

fpassthru

Name

fpassthru — output all remaining data on a file pointer

Description

int fpassthru(int fp);

fputs

fread

Reads to EOF on the given file pointer and writes the results to standard output. If an error occurs, fpassthru returns false.

The file pointer must be valid, and must point to a file successfully opened by fopen, popen, or

fsockopen. The file is closed when fpassthru is done reading it (leaving fp useless).

If you just want to dump the contents of a file to stdout you may want to use the readfile, which saves you the fopen call.

See also readfile, fopen, popen, and fsockopen

Name

fputs — write to a file pointer

Description

int fputs(int fp, string str, int [length]);

fputs is an alias to fwrite, and is identical in every way. Note that the length parameter is optional and if not specified the entire string will be written.

Name

fread — Binary-safe file read

Description

string fread(int fp, int length);

fread reads up to length bytes from the file pointer referenced by fp. Reading stops when length

bytes have been read or EOF is reached, whichever comes first.

// get contents of a file into a string

$filename = "/usr/local/something.txt";

$fd = fopen( $filename, "r" );

$contents = fread( $fd, filesize( $filename ) ); fclose( $fd );

fseek

ftell

See also fwrite, fopen, fsockopen, popen, fgets, fgetss, file, and fpassthru.

Name

fseek — seek on a file pointer

Description

int fseek(int fp, int offset);

Sets the file position indicator for the file referenced by fp to offset bytes into the file stream. Equivalent to calling (in C) fseek( fp, offset, SEEK_SET ).

Upon success, returns 0; otherwise, returns -1. Note that seeking past EOF is not considered an error. May not be used on file pointers returned by fopen if they use the "http://" or "ftp://" formats.

See also ftell and rewind.

Name

ftell — tell file pointer read/write position

Description

int ftell(int fp);

Returns the position of the file pointer referenced by fp; i.e., its offset into the file stream. If an error occurs, returns false.

The file pointer must be valid, and must point to a file successfully opened by fopen or popen. See also fopen, popen, fseek and rewind.

fwrite

is_dir

Name

fwrite — Binary-safe file write

Description

int fwrite(int fp, string string, int [length]);

fwrite writes the contents of string to the file stream pointed to by fp. If the length argument is given, writing will stop after length bytes have been written or the end of string is reached, whichever comes first.

Note that if the length argument is given, then the magic_quotes_runtime configuration option will be ignored and no slashes will be stripped from string.

See also fread, fopen, fsockopen, popen, and fputs.

Name

is_dir — tells whether the filename is a directory

Description

bool is_dir(string filename);

Returns true if the filename exists and is a directory. See also is_file and is_link.

is_executable

Name

is_executable — tells whether the filename is executable

Description

bool is_executable(string filename);

Returns true if the filename exists and is executable. See also is_file and is_link.

is_file

Name

is_file — tells whether the filename is a regular file

Description

bool is_file(string filename);

Returns true if the filename exists and is a regular file. See also is_dir and is_link.

is_link

Name

is_link — tells whether the filename is a symbolic link

Description

bool is_link(string filename);

Returns true if the filename exists and is a symbolic link. See also is_dir and is_file.

is_readable

Name

is_readable — tells whether the filename is readable

Description

bool is_readable(string filename);

Returns true if the filename exists and is readable.

Keep in mind that PHP may be accessing the file as the user id that the web server runs as (often 'nobody'). Safe mode limitations are not taken into account.

See also is_writeable.

is_writeable

Name

is_writeable — tells whether the filename is writeable

Description

bool is_readable(string filename);

link

Returns true if the filename exists and is writeable.

Keep in mind that PHP may be accessing the file as the user id that the web server runs as (often 'nobody'). Safe mode limitations are not taken into account.

See also is_readable.

Name

link — Create a hard link

Description

int link(string target, string link);

Link creates a hard link.

See also the symlink to create soft links, and readlink along with linkinfo.

linkinfo

Name

linkinfo — Get information about a link

Description

int linkinfo(string path);

Linkinfo returns the st_dev field of the UNIX C stat structure returned by the lstat system call. This function is used to verify if a link (pointed to by path) really exists (using the same method as the S_ISLNK macro defined in stat.h). Returns 0 or FALSE in case of error.

See also symlink, link, and readlink.

mkdir

Name

mkdir — make directory

Description

int mkdir(string pathname, int mode);

Attempts to create the directory specified by pathname. Returns true on success and false on failure.

See also rmdir.

pclose

Name

pclose — close process file pointer

Description

int pclose(int fp);

Closes a file pointer to a pipe opened by popen.

The file pointer must be valid, and must have been returned by a successful call to popen. Returns the termination status of the process that was run.

See also popen.

popen

Name

popen — open process file pointer

Description

int popen(string command, string mode);

Opens a pipe to a process executed by forking the command given by command.

Returns a file pointer identical to that returned by fopen, except that it is unidirectional (may only be used for reading or writing) and must be closed with pclose. This pointer may be used with fgets, fgetss, and fputs.

If an error occurs, returns false.

$fp = popen( "/bin/ls", "r" );

See also pclose.

readfile

Name

readfile — output a file

Description

int readfile(string filename);

Reads a file and writes it to standard output.

Returns the number of bytes read from the file. If an error occurs, false is returned and unless the function was called as @readfile, an error message is printed.

If filename begins with "http://" (not case sensitive), an HTTP 1.0 connection is opened to the specified server and the text of the response is written to standard output.

Does not handle HTTP redirects, so you must include trailing slashes on directories.

If filename begins with "ftp://" (not case sensitive), an ftp connection to the specified server is opened and the requested file is written to standard output. If the server does not support passive mode ftp, this will fail.

If filename begins with neither of these strings, the file will be opened from the filesystem and its contents written to standard output.

See also fpassthru, file, and fopen.

readlink

Name

readlink — Return the target of a symbolic link

Description

string readlink(string path);

Readlink does the same as the readlink C function and returns the contents of the symbolic link path or 0 in case of error.

See also symlink, readlink and linkinfo.

rename

Name

rename — rename a file

Description

int rename(string oldname, string newname);

Attempts to rename oldname to newname. Returns true on success and false on failure.

rewind

Name

rewind — rewind the position of a file pointer

Description

int rewind(int fp);

Sets the file position indicator for fp to the beginning of the file stream. If an error occurs, returns 0.

The file pointer must be valid, and must point to a file successfully opened by fopen. See also fseek and ftell.

rmdir

stat

Name

rmdir — remove directory

Description

int rmdir(string dirname);

Attempts to remove the directory named by pathname. The directory must be empty, and the relevant permissions must permit this.

If an error occurs, returns 0. See also mkdir.

Name

stat — give information about a file

Description

array stat(string filename);

Gathers the statistics of the file named by filename.

Returns an array with the statistics of the file with the following elements:

  1. device

  2. inode

  3. number of links

  4. user id of owner

  5. group id owner

  6. device type if inode device *

  7. size in bytes

  8. time of last access

  9. time of last modification

  10. time of last change

  11. blocksize for filesystem I/O *

  12. number of blocks allocated

    • - only valid on systems supporting the st_blksize type--other systems (i.e. Windows) return -1

lstat

Name

lstat — give information about a file or symbolic link

Description

array lstat(string filename);

Gathers the statistics of the file or symbolic link named by filename. This function is identical to the stat function except that if the filename parameter is a symbolic link, the status of the symbolic link is returned, not the status of the file pointed to by the symbolic link.

Returns an array with the statistics of the file with the following elements:

  1. device

  2. inode

  3. number of links

  4. user id of owner

  5. group id owner

  6. device type if inode device *

  7. size in bytes

  8. time of last access

  9. time of last modification

  10. time of last change

  11. blocksize for filesystem I/O *

  12. number of blocks allocated

* - only valid on systems supporting the st_blksize type--other systems (i.e. Windows) return -1

symlink

Name

symlink — Create a symbolic link

Description

int symlink(string target, string link); Symlink creates a symbolic link.

See also link to create hard links, and readlink along with linkinfo.

tempnam

Name

tempnam — create unique file name

Description

string tempnam(string dir, string prefix);

Creates a unique temporary filename.

Returns the new temporary filename, or the null string on failure.

touch

Example 1. tempnam() example

$tmpfname = tempnam( "/tmp", "FOO" );

Name

touch — set modification time of file

Description

int touch(string filename, int time);

Attempts to set the modification time of the file named by filename to the value given by time. If the option time is not given, uses the present time.

If the file does not exist, it is created. Returns true on success and false otherwise.

umask

Name

umask — changes the current umask

Description

int umask(int mask);

Umask sets PHP's umask to mask & 0777 and returns the old umask. When PHP is being used as a server module, the umask is restored when each request is finished.

Umask without arguments simply returns the current umask.

unlink

Name

unlink — Delete a file

Description

int unlink(string filename);

Deletes filename. Similar to the Unix C unlink() function. Returns 0 or FALSE on an error.

See also rmdir for removing directories.