簡體   English   中英

php刪除目錄中的單個文件

[英]php delete a single file in directory

我從這個鏈接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>';
  }
}

列表目錄文件夾調用:備份。
unlink($FileLink) ,當我將鏈接懸停在另一個文件夾到 admin 文件夾時?

unlink('path_to_filename'); 將一次刪除一個文件。

如果目錄中的整個文件都消失了,則意味着您列出了所有文件並在循環中逐個刪除。

好吧,您不能在同一頁面中刪除。 您必須與其他頁面有關。 創建一個名為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>';        
}

在 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

如果不想導航,那就用ajax來優雅吧。

http://php.net/manual/en/function.unlink.php

Unlink 可以安全地刪除單個文件; 只需確保您要刪除的文件實際上是一個文件而不是目錄(“.”或“..”)

if (is_file($filepath))
  {
    unlink($filepath);
  }

你可以簡單地使用它

    $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是適合您的用例的 php 函數。

unlink('/path/to/file');

如果沒有更多信息,我無法告訴您使用它時出了什么問題。

您下載的腳本列出了指定文件夾的內容。 您可能將 unlink - 調用放在列出文件的while循環之一中。

編輯 - 現在您發布了您的代碼:

echo '<a href="'.unlink($FileLink).'"><img src="images/icons/delete.gif"></a></td>';

每次寫入該行時,這樣做都會調用unlink函數,從而刪除您的文件。 您必須編寫一個指向包含刪除函數的腳本的鏈接,並傳遞一些告訴腳本要刪除哪些內容的參數。

例子:

<a href="/path/to/script.php?delete='. $FileLink .'">delete</a>

不應該將路徑傳遞到此腳本的文件並刪除它,因為惡意的人可能會使用它來刪除所有內容或做其他邪惡的事情。

如果您想刪除單個文件,您必須使用unlink()函數,正如您發現的那樣。

該函數將刪除您作為參數傳遞的內容:因此,您可以將其傳遞它必須刪除的文件路徑


例如,您將使用以下內容:

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 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);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM