I've got the php list directory script from this link http://www.gaijin.at/en/scrphpfilelist.php.
How do I delete a single file from the directoy? I tried unlink
, but it deleted all the files from that directory. this the short code what i got from the link!
while ($file = readdir ($hDir)) {
if ( ($file != '.') && ($file != '..') && (substr($file, 0, 1) != '.') &&
(strtolower($file) != strtolower(substr($DescFile, -(strlen($file))))) &&
(!IsFileExcluded($Directory.'/'.$file))
) {
array_push($FilesArray, array('FileName' => $file,
'IsDir' => is_dir($Directory.'/'.$file),
'FileSize' => filesize($Directory.'/'.$file),
'FileTime' => filemtime($Directory.'/'.$file)
));
}
}
$BaseDir = '../_cron/backup';
$Directory = $BaseDir;
foreach($FilesArray as $file) {
$FileLink = $Directory.'/'.$file['FileName'];
if ($OpenFileInNewTab) $LinkTarget = ' target="_blank"'; else $LinkTarget = '';
echo '<a href="'.$FileLink.'">'.$FileName.'</a>';
echo '<a href="'.unlink($FileLink).'"><img src="images/icons/delete.gif"></a></td>';
}
}
the list directory folder call : backup.
in the unlink($FileLink)
, when i hover the link has change to another folder to admin folder?
To delete a file in PHP, use the unlink function. Let's go through an example to see how it works. The first argument of the unlink function is a filename which you want to delete. The unlink function returns either TRUE or FALSE , depending on whether the delete operation was successful.
PHP | unlink() Function The unlink() function is an inbuilt function in PHP which is used to delete files.
If you want to delete everything from folder (including subfolders) use this combination of array_map , unlink and glob : array_map( 'unlink', array_filter((array) glob("path/to/temp/*") ) );
http://php.net/manual/en/function.unlink.php
Unlink can safely remove a single file; just make sure the file you are removing it actually a file and not a directory ('.' or '..')
if (is_file($filepath))
{
unlink($filepath);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With