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 dire开发者_如何转开发ctory. 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 theunlink($FileLink)
, when i hover the link has change to another folder to admin folder?unlink('path_to_filename');
will delete one file at a time.
If your whole files from directory is gone means you listed all files and deleted one by one in a loop.
Well you cannot de delete in the same page. You have to do with other page. create a page called deletepage.php
which will contain script to delete and link to that page with 'file' as parameter.
foreach($FilesArray as $file)
{
$FileLink = $Directory.'/'.$file['FileName'];
if($OpenFileInNewTab) $LinkTarget = ' target="_blank"';
else $LinkTarget = '';
echo '<a href="'.$FileLink.'">'.$FileName.'</a>';
echo '<a href="deletepage.php?file='.$fileName.'"><img src="images/icons/delete.gif"></a></td>';
}
On the deletepage.php
//and also consider to check if the file exists as with the other guy suggested.
$filename = $_GET['file']; //get the filename
unlink('DIRNAME'.DIRECTORY_SEPARATOR.$filename); //delete it
header('location: backto prev'); //redirect back to the other page
If you don't want to navigate, then use ajax to make elegant.
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);
}
Simply You Can Use It
$sql="select * from tbl_publication where id='5'";
$result=mysql_query($sql);
$res=mysql_fetch_array($result);
//Getting File Name From DB
$pdfname = $res1['pdfname'];
//pdf is directory where file exist
unlink("pdf/".$pdfname);
unlink is the right php function for your use case.
unlink('/path/to/file');
Without more information, I can't tell you what went wrong when you used it.
The script you downloaded lists the content of a specified folder. You probably put the unlink - call in one of the while
-loops that list the files.
EDIT - Now that you posted your code:
echo '<a href="'.unlink($FileLink).'"><img src="images/icons/delete.gif"></a></td>';
Doing this calls the unlink
-function each time the line is written, deleting your file.
You have to write a link to a script that contains a delete function and pass some parameter that tells your script what to delete.
Example:
<a href="/path/to/script.php?delete='. $FileLink .'">delete</a>
You should not pass the path to a file this script and just delete it though, because malevolent being might use it to just delete everything or do other evil things.
If you want to delete a single file, you must, as you found out, use the unlink()
function.
That function will delete what you pass it as a parameter : so, it's up to you to pass it the path to the file that it must delete.
For example, you'll use something like this :
unlink('/path/to/dir/filename');
<?php
if(isset($_GET['delete'])){
$delurl=$_GET['delete'];
unlink($delurl);
}
?>
<?php
if ($handle = opendir('.')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
echo "<a href=\"$entry\">$entry</a> | <a href=\"?delete=$entry\">Delete</a><br>";
}
}
closedir($handle);
}
?>
This is It
// This code was tested by me (Helio Barbosa)
// this directory (../backup) is for try only.
// it is necessary create it and put files into him.
$hDir = '../backup';
if ($handle = opendir( $hDir )) {
echo "Manipulador de diretório: $handle\n";
echo "Arquivos:\n";
/* Esta é a forma correta de varrer o diretório */
/* Here is the correct form to do find files into the directory */
while (false !== ($file = readdir($handle))) {
// echo($file . "</br>");
$filepath = $hDir . "/" . $file ;
// echo( $filepath . "</br>" );
if(is_file($filepath))
{
echo("Deleting:" . $file . "</br>");
unlink($filepath);
}
}
closedir($handle);
}
精彩评论