SQL PHP HTML ASP JavaScript articles and free scripts to download
PHP File Function

PHP File delete using unlink function

We can delete files by giving its URL or path in PHP by using unlink command. This command will work only if write permission is given to the folder or file. Without this the delete command will fail. Here is the command to delete the file.
We can check the path by using file_exists function to know if file is available for deletion. Here $path is the relative path of the file calculated from the script execution. Here is an example of deleting file by using relative path
if(unlink($path)) echo "Deleted file ";
We have used if condition to check whether the file delete command is successful or not. But the command below will not work.
if(unlink($path)) echo "Deleted file ";
The warning message will say unlink() [function.unlink]: HTTP does not allow unlinking.

We can suppress the warning message by adding a @ symbol before the unlink command .
Based on the success of the file delete command we can further execute our code by using if else code block.
if(@unlink($path)) {echo "Deleted file "; }
else{echo "File can't be deleted";}
Here we are deleting one file only. We can also delete all the files of a directory or selected files by using PHP.

Deleting all ( or by selection ) files of a directory

Number of User Comments : 4

amazing tutorils.... thanks
how to generate a Multiple deletion method
sir i make a page which delete a record but a file or image that are stored in the folder did not removed . How to remove from folder a picture using unlink()
Post Comment This is for short comments only. Use the forum for more discussions.
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked

HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us

©2000-2015 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer