php php删除目录中的单个文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5338123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php delete a single file in directory
提问by tonoslfx
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!
我从这个链接http://www.gaijin.at/en/scrphpfilelist.php得到了 php 列表目录脚本。如何从目录中删除单个文件?我试过unlink
,但它删除了该目录中的所有文件。这是我从链接中得到的短代码!
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?
列表目录文件夹调用:备份。
在unlink($FileLink)
,当我悬停时,链接已更改为另一个文件夹到 admin 文件夹?
回答by Santosh Linkha
unlink('path_to_filename');
will delete one file at a time.
unlink('path_to_filename');
将一次删除一个文件。
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.
好吧,您不能在同一页面中删除。您必须与其他页面有关。创建一个名为的页面deletepage.php
,该页面将包含要删除的脚本并使用“文件”作为参数链接到该页面。
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
在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.
如果不想导航,那就用ajax来优雅吧。
回答by Tramov
http://php.net/manual/en/function.unlink.php
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 '..')
Unlink 可以安全地删除单个文件;只需确保您要删除的文件实际上是一个文件而不是目录(“.”或“..”)
if (is_file($filepath))
{
unlink($filepath);
}
回答by Surendra Jnawali
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);
回答by middus
回答by svanelten
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.
您下载的脚本列出了指定文件夹的内容。您可能将 unlink - 调用放在while
列出文件的循环之一中。
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.
unlink
每次写入该行时,这样做都会调用- 函数,删除您的文件。您必须编写一个指向包含删除函数的脚本的链接,并传递一些告诉脚本要删除哪些内容的参数。
Example:
例子:
<a href="/path/to/script.php?delete='. $FileLink .'">delete</a>
You should notpass 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.
您不应该将路径传递到此脚本的文件并删除它,因为恶意的人可能会使用它来删除所有内容或做其他邪恶的事情。
回答by Pascal MARTIN
If you want to delete a single file, you must, as you found out, use the unlink()
function.
如果您想删除单个文件,您必须像您发现的那样使用该unlink()
功能。
That function will delete what you pass it as a parameter : so, it's up to you to pass it the path to the filethat it must delete.
该函数将删除您作为参数传递的内容:因此,您可以将其传递给它必须删除的文件的路径。
For example, you'll use something like this :
例如,您将使用以下内容:
unlink('/path/to/dir/filename');
回答by saiful islam
<?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
就是这个