Delete File or an Image from the Server using PHP
If you want to delete a file or an image from the server via an interface from you website, you can use the following code
<?php
function deletemyfile($filename)
{
$filearray = explode("/", $filename);
$filecountno = count($filearray);
$filecountno = $filecountno-1;
for($i=0;$i<=$filecountno;$i++)
{
$myfilename = $filearray[$i];
}
if(file_exists("$filename"))
{
if(unlink("$filename"))
{
$deleteresult = "<b>".$myfilename."</b>: File was deleted successfully.";
}
else
{
$deleteresult = "<font color='red'><b>".$myfilename."</b>: Error! You may not have enough permission to delete this file.</font>";
}
}
else
{
$deleteresult = "<font color='red'><b>".$myfilename."</b>: Error! Could not delete file. File does not exist.</font>";
}
return $deleteresult;
}
// Calling the function
$filename = '../photos/aaa.jpg';
$callresult = "<b>File Deletion Log:</b><br>";
$callresult = $callresult."<br>".deletemyfile($filename);
echo "$callresult";
?>
Related Articles
- Add an Automatic / Dynamic Copyrighted Year Message in PHP
- Php Mail Function with Html Header
- How to check if an email address exists without sending an email?
- Pass Array from Multipart/Form-data Post Method or Query String with Foreach Function in PHP
- Use in_array function to check if a value exists in an array using PHP
- Loop through an Array using Foreach Function in PHP
- Encrypt Decrypt using the Rijndael 256 Mcrypt Function in PHP
- Encrypt Decrypt using the Rijndael 256 Mcrypt Class in PHP
