PHP - File system manipulation
Creating Directories:
To create a directory, you provide the path where you want the new directory to be created. For example:
$directoryPath = 'new_directory';
if (!is_dir($directoryPath)) {
if (mkdir($directoryPath, 0777, true)) {
echo "Directory created successfully.";
} else {
echo "Failed to create directory.";
}
} else {
echo "Directory already exists.";
}
Checking Existing Directory:
Before creating a directory, it's a good practice to check whether the directory already exists using is_dir(). This prevents accidentally overwriting existing directories.
Permission Considerations:
The permissions specified in the $mode parameter determine who can read, write, and execute within the newly created directory. It's important to set appropriate permissions to ensure security and access control. Using 0777 (full permissions) should be avoided in production environments.
Recursive Directory Creation:
The $recursive parameter allows you to create not only the specified directory but also any parent directories that do not exist. This is especially useful when creating nested directory structures.
Error Handling:
In the code example, there's error handling to notify you whether the directory was successfully created or if an error occurred during the creation process.
Creating Files :
Creating, opening, and renaming files are common file manipulation tasks in PHP. Here's how you can perform these tasks using the touch(), fopen(), and rename() functions:
Using touch() to Create Files:
The touch() function is often used to update file timestamps, but it can also be used to create an empty file if the file doesn't exist.
$filePath = 'new_file.txt';
if (touch($filePath)) {
echo "File created successfully using touch().";
} else {
echo "Failed to create file.";
}
Note that this method creates an empty file without any content.
Using fopen() to Create and Write Files:
The fopen(), fwrite(), and fclose() combination allows you to create a file and write content to it.
$filePath = 'new_file.txt';
$content = "Hello, world!";
$file = fopen($filePath, 'w');
if ($file) {
if (fwrite($file, $content) !== false) {
fclose($file);
echo "File created and written successfully using fopen().";
} else {
fclose($file);
echo "Failed to write content to file.";
}
} else {
echo "Failed to create file.";
}
This method not only creates the file but also allows you to write content into it.
Using rename() to Rename Files:
The rename() function is used to rename files.
$oldFilePath = 'old_file.txt';
$newFilePath = 'new_file_name.txt';
if (rename($oldFilePath, $newFilePath)) {
echo "File renamed successfully using rename().";
} else {
echo "Failed to rename file.";
}
The rename() function changes the name of the file from $oldFilePath to $newFilePath.
When working with files, remember to consider file permissions and error handling to ensure smooth and secure operations. Additionally, be cautious when renaming files, as this action cannot be undone and might lead to loss of data if not performed carefully.
Deleting Files/Directories :
Deleting Files/DirectoriesDeleting files and directories is a common file management task in PHP. It's important to be cautious when deleting files and directories, as irreversible data loss can occur. Here's how you can delete files and directories using the unlink() and rmdir() functions:
Deleting Files with unlink():
The unlink() function is used to delete files. It removes the specified file from the filesystem.
$filePath = 'file_to_delete.txt';
if (unlink($filePath)) {
echo "File deleted successfully using unlink().";
} else {
echo "Failed to delete file.";
}
Remember that this operation is irreversible, and the file will be permanently deleted.
Deleting Empty Directories with rmdir():
The rmdir() function is used to delete empty directories. It removes the specified empty directory from the filesystem.
$directoryPath = 'directory_to_delete';
if (rmdir($directoryPath)) {
echo "Directory deleted successfully using rmdir().";
} else {
echo "Failed to delete directory.";
}
You can only use rmdir() to delete empty directories. If the directory is not empty, the function will fail.
Deleting Directories and Their Contents:
To delete non-empty directories and their contents, you can use third-party libraries or iterate through the directory contents and delete files and subdirectories using the appropriate functions.
function deleteDirectory($dir) {
if (!is_dir($dir)) {
return;
}
$files = array_diff(scandir($dir), ['.', '..']);
foreach ($files as $file) {
$path = $dir . '/' . $file;
if (is_dir($path)) {
deleteDirectory($path);
} else {
unlink($path);
}
}
rmdir($dir);
}
$directoryPath = 'directory_to_delete';
deleteDirectory($directoryPath);
The deleteDirectory() function recursively deletes files and subdirectories within the specified directory before finally removing the empty directory.