Common PHP File and Directory Utility Functions
This article provides a collection of reusable PHP functions for checking, creating, listing, copying, and deleting files and directories, offering developers ready‑to‑use code snippets that simplify common filesystem operations in backend projects.
Below is a set of handy PHP functions that can be directly used in backend development to manage files and directories, reducing the amount of repetitive code you need to write.
1. Check if a directory exists
function check_dir($path, $create = false) {
if (is_dir($path)) {
return true;
}
return false;
}2. Create a directory
function create_dir($path) {
if (!file_exists($path)) {
if (mkdir($path, 0777, true)) {
return true;
}
}
return false;
}3. Check if a file exists
function check_file($path, $create = false, $content = null) {
if (file_exists($path)) {
return true;
}
return false;
}4. Create a file
function create_file($path, $content = null, $over = false) {
if (file_exists($path) && !$over) {
return false;
} elseif (file_exists($path)) {
@unlink($path);
}
check_dir(dirname($path), true);
$handle = fopen($path, 'w') or error('创建文件失败,请检查目录权限!');
fwrite($handle, $content);
return fclose($handle);
}5. List sub‑directories
function dir_list($path) {
$list = array();
if (!is_dir($path) || !$filename = scandir($path)) {
return $list;
}
$files = count($filename);
for ($i = 0; $i < $files; $i++) {
$dir = $path . '/' . $filename[$i];
if (is_dir($dir) && $filename[$i] != '.' && $filename[$i] != '..') {
$list[] = $filename[$i];
}
}
return $list;
}6. List files in a directory
function file_list($path) {
$list = array();
if (!is_dir($path) || !$filename = scandir($path)) {
return $list;
}
$files = count($filename);
for ($i = 0; $i < $files; $i++) {
$dir = $path . '/' . $filename[$i];
if (is_file($dir)) {
$list[] = $filename[$i];
}
}
return $list;
}7. List both files and sub‑directories
function path_list($path) {
$list = array();
if (!is_dir($path) || !$filename = scandir($path)) {
return $list;
}
$files = count($filename);
for ($i = 0; $i < $files; $i++) {
$dir = $path . '/' . $filename[$i];
if (is_file($dir) || (is_dir($dir) && $filename[$i] != '.' && $filename[$i] != '..')) {
$list[] = $filename[$i];
}
}
return $list;
}8. Delete a directory and its contents (or delete a single file)
/**
* Delete a directory and all its files, or delete a specific file
* @param string $path Path to delete
* @param bool $delDir Whether to delete the directory itself (true) or keep it (false)
* @return bool Deletion status
*/
function path_delete($path, $delDir = false, $exFile = array()) {
$result = true; // true for empty directories
if (!file_exists($path)) {
return $result;
}
if (is_dir($path)) {
if ($dirs = scandir($path)) {
foreach ($dirs as $value) {
if ($value != '.' && $value != '..' && !in_array($value, $exFile)) {
$dir = $path . '/' . $value;
$result = is_dir($dir) ? path_delete($dir, $delDir, $exFile) : unlink($dir);
}
}
if ($result && $delDir) {
return rmdir($path);
} else {
return $result;
}
} else {
return false;
}
} else {
return unlink($path);
}
}9. Copy a directory recursively
function dir_copy($src, $des, $son = 1) {
if (!is_dir($src)) {
return false;
}
if (!is_dir($des)) {
create_dir($des);
}
$handle = dir($src);
while ($path = $handle->read()) {
if ($path != '.' && $path != '..') {
if (is_dir($src . '/' . $path)) {
if ($son) {
dir_copy($src . '/' . $path, $des . '/' . $path, $son);
}
} else {
copy($src . '/' . $path, $des . '/' . $path);
}
}
}
return true;
}Feel free to copy these snippets into your projects; they can save you time when handling common filesystem tasks.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.