PHP | unlink() Function Last Updated : 09 Apr, 2025 Comments Improve Suggest changes Like Article Like Report The unlink() function in PHP is used to delete a file from the filesystem. It does not delete directories. For that, you would need to use rmdir() or other methods.Syntax:unlink( $filename, $context )In this syntax:$filename: It is a mandatory parameter that specifies the filename of the file that has to be deleted.$context: It is an optional parameter that specifies the context of the file handle, which can be used to modify the nature of the stream.Return Value: It returns True on success and False on failure.How the unlink() Function Works?The unlink() function works in the following ways:File Deletion: The unlink() function removes the file from the filesystem permanently. Once deleted, the file cannot be recovered unless a backup is available.Permissions: The script must have appropriate file permissions to delete the file. If the file is owned by another user or has read-only permissions, unlink() will fail.Directory Deletion: unlink() only works for files. If you try to delete a directory with unlink(), it will fail. For deleting directories, use the rmdir() function (which only works for empty directories).Examples of Using the unlink() Function1. Deleting a FileHere’s a basic example of how to use the unlink() function to delete a file. PHP <?php $file = "example.txt"; // Check if the file exists before attempting to delete it if (file_exists($file)) { if (unlink($file)) { echo "The file $file has been deleted."; } else { echo "Error: The file $file could not be deleted."; } } else { echo "Error: The file $file does not exist."; } ?> Output:php unlink() functionIn this example:First, the code checks if the file exists using file_exists().If the file exists, unlink() is called to delete it. If successful, a success message is displayed. If it fails, an error message is shown.If the file doesn’t exist, an error message is displayed.2. Deleting an Uploaded FileIf your application allows users to upload files and you need to delete a file after some time (for example, after processing), you can use unlink(). PHP <?php $uploadedFile = "uploads/image.jpg"; // Delete the uploaded file after processing if (unlink($uploadedFile)) { echo "The uploaded file has been deleted."; } else { echo "Failed to delete the uploaded file."; } ?> Output:php unlink() functionIn this example:This example deletes a file from the uploads/ directory.You would use this after completing some operation (like processing or storing the data) and want to remove the uploaded file.Best Practices When Using unlink()Check If the File Exists: Always verify if the file exists using file_exists() before calling unlink() to prevent unnecessary errors or warnings.Check Permissions: Ensure that your script has permission to delete the file. If the file is not writable or owned by another user, unlink() will fail. Use is_writable() to check permissions before trying to delete the file.Error Handling: Proper error handling should be in place to manage scenarios where the file cannot be deleted. This helps provide useful feedback to users or log the issue for further investigation.Security Considerations: Avoid allowing users to directly provide file names or paths to unlink(), as this can lead to directory traversal vulnerabilities. Always sanitize the file paths and validate user input.ConclusionThe unlink() function in PHP is a straightforward and useful way to remove files from the filesystem. Whether you're managing uploaded files, cleaning up temporary files, or simply performing file maintenance, unlink() provides an efficient means of deletion. However, it’s important to handle errors gracefully, check file permissions, and ensure that you're deleting the correct files to avoid data loss. Comment More infoAdvertise with us Next Article PHP | unlink() Function S Shubrodeep Banerjee Follow Improve Article Tags : Misc Web Technologies PHP PHP-file-handling PHP-function +1 More Practice Tags : Misc Similar Reads PHP basename( ) Function The basename() function in PHP is an inbuilt function which is used to return the base name of a file if the path of the file is provided as a parameter to the basename() function. Syntax: string basename ( $path , $suffix ) Parameters: The basename() function in PHP accepts two parameters which are 2 min read PHP chgrp( ) Function The chgrp() function in PHP is an inbuilt function that is used to change the user group of the specified file. It returns true on success and false on failure. Only the superuser has the right to change the group of a file arbitrarily. Syntax: bool chgrp ( $filename, $group ) Parameters: The chgrp( 2 min read PHP chmod( ) Function The chmod() function in PHP is an inbuilt function which is used to change the mode of a specified file to a specific mode given by the user. The chmod() function changes the permissions of the specified file and returns true on success and false on failure. Syntax: bool chmod ( string $filename, i 2 min read PHP chown( ) Function The chown() function in PHP is an inbuilt function which is used to change the owner of the specified file. It returns true on success and false on failure. Only the superuser has the right to change the owner of a file. Syntax: bool chown ( $filename, $user ) Parameters: The chown() function in PHP 2 min read PHP copy( ) Function The copy() function in PHP is an inbuilt function which is used to make a copy of a specified file. It makes a copy of the source file to the destination file and if the destination file already exists, it gets overwritten. The copy() function returns true on success and false on failure. Syntax: bo 2 min read PHP dirname( ) Function The dirname() function in PHP is an inbuilt function which is used to return the directory name of a given path. The dirname() function is used to parent directory's path i.e levels up from the current directory. The dirname() function returns the path of a parent directory which includes a dot ('.' 2 min read PHP disk_free_space( ) Function The disk_free_space() function in PHP is an inbuilt function which is used to return the amount of free space in a specified directory. The disk_free_space() function denotes the free space in bytes. It returns the available space on a filesystem or on a disk partition. The disk_free_space() functio 2 min read PHP disk_total_space( ) Function The disk_total_space() function in PHP is an inbuilt function which is used to return the total space of a specified directory. The disk_total_space() function denotes the total space in bytes. It returns the total space on a filesystem or on a disk partition. The disk_total_space() function returns 2 min read PHP fclose() Function The fclose() function in PHP closes a file that was previously opened by fopen(). Closing a file releases the resource associated with it and makes sure all the data written to the file is properly saved. Not closing files can lead to resource leaks or incomplete data writing.Syntax:bool fclose(reso 2 min read PHP feof( ) Function The feof() function in PHP is an inbuilt function which is used to test for the end-of-file on a file pointer. It checks if the "end-of-file" has been reached or not. The feof() function is used for looping through the content of a file if the size of content is not known beforehand. The feof() func 2 min read Like