
#Hogwasher delete multiple nzbs code#
If you have multiple files that you want to delete and have their names saved in a database table, or in an array, you can iterate the above code inside a while or a foreach loop like in the example below. Else, it will return an error message that the file doesn't exist. The code below executes the deletion only after we have confirmed that the file exists. In the same way as the unlink() function, pass the file path if the file is not in the same directory as the PHP script. It returns true if the file exists or false if no such file exists in the specified directory. We use the in-built PHP file_exists() function to do the check. It is therefore important to first check whether the file already exists before trying to delete it. Warning: unlink(images/logos.png): No such file or directory in /opt/lampp/htdocs/www/demos/filedeletion.php on line 3 If you try to delete a file that does not exist in the directory, you will get the error below. You can add an if statement to the code above to display a success message on successful deletion, or a failure message on deletion error as below: Warning: unlink(images/logo.png): Permission denied in /opt/lampp/htdocs/www/demos/filedeletion.php on line 3 If you don't have that permission you will get the error below: Please note you need to have write permission to the directory in which the file to be deleted is located. Running the above code completely deletes the "logo.png" file from the "images" folder. So I have added the code below into the "filedeletion.php" file. The relative path to the file "logo.png" from the PHP script is "images/logo.png". I have as well placed a file namely "logo.png" and placed it in the directory path: "/opt/lampp/htdocs/www/demos/images/" as shown below: I have created a PHP file namely "filedeletion.php" and placed it in the directory path: "/opt/lampp/htdocs/www/demos/" as shown below: If the file to be deleted happens not to be in the same directory with the PHP script then you need to include the relative path to in the unlink() function as in the example below. The above code deletes a file named image.jpg placed in the same directory as the PHP file with the code. All you need is to pass the name of the file to the function and it will be deleted. To delete a single file, use the inbuilt PHP unlink() function.


In this article, you will learn multiple methods in which you can delete single or multiple files in a given directory programmatically using PHP. Luckily, you can delete them programmatically with a PHP script. This can be an overwhelming task especially if they have piled up to huge numbers and want to select and delete specific ones with a certain filename, or those that meet certain criteria. Over time, you may get to a point where you no longer need some of these files and want to delete them. In your day-to-day web projects, you happen to work with multiple files of different types such as.
#Hogwasher delete multiple nzbs how to#
John Mwaniki / How to delete single or multiple files in a directory using PHP
