Here are some basic functions we can use when working with files in PHP.
We can use the function scandir()
to list all files and directories of a specific path.
Syntax
scandir(directory, order)
order:
We can specify the sorting order. The default sort order is ascending order (0).
Example
$dir = scandir(__DIR__);
var_dump($dir);
// array(4) { [0]=> string(1) "." [1]=> string(2) ".." [2]=> string(9) ".DS_Store" [3]=> string(9) "index.php" }
We can check whether a specific filename is a file or a directory using is_file()
or is_dir()
.
Syntax
// Check if it's a file
is_file(file)
// Check if it's a directory
is_dir(file)
Example
$dir = scandir(__DIR__);
var_dump(is_file($dir[2])); // bool(true)
var_dump(is_dir($dir[2])); // bool(false)
We can check if a file or a directory exists using the file_exists()
function. This function’s result is cached.
Syntax
file_exists(path)
Example
if (file_exists("text.txt")) {
echo filesize("text.txt");
} else {
echo "File not found";
} // File not found
PHP cache the return value of some file-related functions for better performance.
Affected functions: stat(), lstat(), file_exists(), is_writable(), is_readable(), is_executable(), is_file(), is_dir(), is_link(), filectime(), fileatime(), filemtime(), fileinode(), filegroup(), fileowner(), filesize(), filetype(), and fileperms().
We can use clearstatcache()
to clear the cache.
Syntax
clearstatcache()
We can create a new directory using the mkdir()
function.
Syntax
mkdir(path);
Example
mkdir("partials");
We can delete a directory using the rmdir()
function.
Syntax
rmdir(dir)
Example
rmdir("partials");
We can open a file using the fopen()
function.
Syntax
fopen(filename, mode)
There are several modes for fopen.
Example
$file = fopen("text.txt", "r");
Syntax
We use the fclose()
function to close an open file.
fclose(file)
Example
$file = fopen("text.txt", "r");
fclose($file);
We can check if the “end-of-file” is reached for an open file usingfeof()
.
It’s useful in loops.
We can use the fgets()
function to return a line from an open file.
Syntax
fgets(file)
Example
$file = fopen("text.txt","r");
while(! feof($file)) {
echo fgets($file). "<br>";
}
fclose($file);
The fgetcsv() function parses a line from an open file and returns an array.
Syntax
fgetcsv(file, length, separator)
Example
$file = fopen("text.txt","r");
while(! feof($file)) {
echo "<pre>";
print_r(fgetcsv($file));
echo "</pre>";
}
fclose($file);
/* Array
(
[0] => a
[1] => b
[2] => c
)
Array
(
[0] => d
[1] => e
[2] => f
)
Array
(
[0] => g
[1] => h
[2] => i
) */
We can read file content using the file_get_contents()
function.
Syntax
file_get_contents(path, include_path, start, max_length)
Example
echo file_get_contents("text.txt", offset: 2, length: 3);
// llo
We can write content to a file using the file_put_contents()
function.
Syntax
file_put_contents(filename, data, mode, )
Example
echo file_put_contents("text.txt","Testing!");
unlink()
.copy()
. If the to_fine file already exists, it will be overwritten.rename()
.Syntax
unlink(filename)
copy(from_file, to_file)
rename(old, new)
Example
unlink("text.txt");
echo copy("from.txt","to.txt");
rename("/partials/file1.txt","/docs/file2.txt");
We can check information about a file path using the pathinfo()
function.
Syntax
pathinfo(path, options)
Option values
Example
print_r(pathinfo("/docs/text.txt"));