PHP | Filesystem Functions

PHP Filesystem Functions


Hello dear readers! welcome back to another edition of our tutorial on PHP. In this tutorial guide, we are going to be discussing about the PHP Filesystem Functions.

The PHP filesystem functions are used to access and manipulate the filesystem. PHP provides you all the possible functions you may need to manipulate a file.

Installation

There is no Installation needed to use these functions; they are part of the PHP core.


Runtime Configuration

The behaviour of these functions is affected by settings in php.ini.

NameDefaultChangeableChangelog
allow_url_fopen"1"PHP_INI_ALLPHP_INI_ALL in PHP <= 4.3.4. PHP_INI_SYSTEM in PHP < 6. Available since PHP 4.0.4.
allow_url_include"0"PHP_INI_ALLPHP_INI_SYSTEM in PHP 5. Available since PHP 5.2.0.
user_agentNULLPHP_INI_ALLAvailable since PHP 4.0.3.
default_socket_timeout"60"PHP_INI_ALLAvailable since PHP 4.3.0.
from""PHP_INI_ALL
auto_detect_line_endings"0"PHP_INI_ALLAvailable since PHP 4.3.0.

List of Filesystem Functions

The following below is the list of filesystem functions supported in PHP. PHP - indicates the earliest versions of PHP that have support for the functions.

Sr.NoFunction & DescriptionPHP
1basename()

Returns filename component of path

3
2chgrp()

Changes file group

3
3chmod()

Changes file mode

3
4chown()

Changes file owner

3
5clearstatcache()

Clears file status cache

3
6copy()

Copies file

3
7delete()

Deletes file

8dirname()

Returns directory name component of path

3
9disk_free_space()

Returns available space in directory

4.0.7
10disk_total_space()

Returns the total size of a directory

4.0.7
11diskfreespace()

Alias of disk_free_space()

4.0.7
12fclose()

Closes an open file pointer

3
13feof()

Tests for end-of-file on a file pointer

3
14fflush()

Flushes the output to a file

4
15fgetc()

Gets character from file pointer

3
16fgetcsv()

Gets line from file pointer and parse for CSV fields

3
17fgets()

Gets line from file pointer

3
18fgetss()

Gets line from file pointer and strip HTML tags

3
19file_exists()

Checks whether a file or directory exists

3
20file_get_contents()

Reads entire file into a string

4.3.0
21file_put_contents()

Write a string to a file

5
22file()

Reads entire file into an array

3
23fileatime()

Gets last access time of file

3
24filectime()

Gets inode change time of file

3
25filegroup()

Gets file group

3
26fileinode()

Gets file inode

3
27filemtime()

Gets file modification time

3
28fileowner()

Gets file owner

3
29fileperms()

Gets file permissions

3
30filesize()

Gets file size

3
31filetype()

Gets file type

3
32flock()

Portable advisory file locking

3
33fnmatch()

Match filename against a pattern

4.0.3
34fopen()

Opens file or URL

3
35fpassthru()

Output all remaining data on a file pointer

3
36fputcsv()

Format line as CSV and write to file pointer

5.1.0
37fputs()

Alias of fwrite()

3
38fread()

Binary-safe file read

3
39fscanf()

Parses input from a file according to a format

4.0.1
40fseek()

Seeks on a file pointer

3
41fstat()

Gets information about a file using an open file pointer

4
42ftell()

Tells file pointer read/write position

3
43ftruncate()

Truncates a file to a given length

4
44fwrite()

Binary-safe file write

3
45glob()

Find pathnames matching a pattern

4.0.3
46is_dir()

Tells whether the filename is a directory

3
47is_executable()

Tells whether the filename is executable

3
48is_file()

Tells whether the filename is a regular file

3
49is_link()

Tells whether the filename is a symbolic link

3
50is_readable()

Tells whether the filename is readable

3
51is_uploaded_file()

Tells whether the file was uploaded via HTTP POST

4.0.3
52is_writable()

Tells whether the filename is writable

3
53is_writeable()

Alias of is_writable()

3
54lchgrp()

Changes group ownership of symlink

5.1.2
55lchown()

Changes user ownership of symlink

5.1.2
56link()

Create a hard link

3
57linkinfo()

Gets information about a link

3
58lstat()

Gives information about a file or symbolic link

3
59mkdir()

Makes directory

3
60move_uploaded_file()

Moves an uploaded file to a new location

4.0.3
61parse_ini_file()

Parse a configuration file

4
62pathinfo()

Returns information about a file path

4.0.3
63pclose()

Closes process file pointer

3
64popen()

Opens process file pointer

3
65readfile()

Outputs a file

3
66readlink()

Returns the target of a symbolic link

3
67realpath()

Returns canonicalized absolute pathname

4
68rename()

Renames a file or directory

3
69rewind()

Rewind the position of a file pointer

3
70rmdir()

Removes directory

3
71set_file_buffer()

Alias of stream_set_write_buffer()

3
72stat()

Gives information about a file

3
73symlink()

Creates a symbolic link

3
74tempnam()

Create file with unique file name

3
75tmpfile()

Creates a temporary file

3
76touch()

Sets access and modification time of file

3
77umask()

Changes the current umask

3
78unlink()

Deletes a file

3

In our subsequent tutorials, we are going to be discussing about the above listed functions.

Filesystem Constants

The following below is the list of filesystem constants supported in PHP. PHP - indicates the earliest versions of PHP that have support for these constants.

You can make use of any of these constants while configuring your php.ini file.

Sr.NoConstant & DescriptionPHP
1

GLOB_BRACE

2

GLOB_ONLYDIR

3

GLOB_MARK

4

GLOB_NOSORT

5

GLOB_NOCHECK

6

GLOB_NOESCAPE

7

PATHINFO_DIRNAME

8

PATHINFO_BASENAME

9

PATHINFO_EXTENSION

10

PATHINFO_FILENAME

5.2.0
11

FILE_USE_INCLUDE_PATH

Search for filename in include_path

5.0.0
12

FILE_APPEND

Append content to existing file.

13

FILE_IGNORE_NEW_LINES

Strip EOL characters

5.0.0
14

FILE_SKIP_EMPTY_LINES

Skip empty lines

5.0.0
15

FILE_BINARY

Binary mode

6.0.0
16

FILE_TEXT

Text mode

6.0.0


Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial guide, we are going to be discussing about the PHP basename() Function.

Do feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.