File Deletion operation in PHP Flashcards
Definition of File Deletion in PHP
What is file deletion in PHP?
File deletion in PHP is performed using the unlink() function, which removes a specified file from the filesystem.
Syntax of unlink()
What is the syntax of the unlink() function in PHP?
unlink(file, context);
Parameters of unlink()
What are the parameters of the unlink() function?
file (Required): Specifies the file to delete with its path.
context (Optional): Defines the meaning of the file handle
Example of File Deletion
Provide an example of using unlink() to delete a file in PHP.
<?php
// Create a sample file
$samplefile = fopen(“file.txt”, “w”);
fwrite($samplefile, “Hello Friends!”);
fclose($samplefile);
// Delete the file
if (unlink(“file.txt”)) {
echo “File deleted successfully.”;
} else {
echo “Error deleting the file.”;
}
?>
Return Value of unlink()
What does the unlink() function return?
Returns TRUE if the file was successfully deleted.
Returns FALSE if the file could not be deleted (e.g., file does not exist or insufficient permissions).
Important Notes on unlink()
What are some important notes regarding the unlink() function?
Permanent Deletion:unlink() permanently deletes the file; it does not move it to the Recycle Bin.
File Permissions: Ensure the script has permission to delete the file.
PHP Version: Available in PHP version 4.0 and higher.