|
|
PHP File delete using unlink functionWe 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.
unlink($path);
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
$path="images/all11.css";
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.
$path="http://domainname/file/red.jpg";
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.
$path="test.html";
if(@unlink($path)) {echo "Deleted file "; }
else{echo "File can't be deleted";}
|
|
| hariv21 | 31-03-2010 |
|---|
| nice | | php | 29-09-2012 |
|---|
amazing tutorils....
thanks |
|
|
|
|
|
|