2017-05-04 102 views
-1

如何從文件夾中刪除單個文件?當我點擊我的刪除鏈接時,它會從文件夾中刪除所有上傳的文件。如何從文件夾中刪除單個文件?

鏈接:

<td><a href="Ad_delete.php?$Id=<?php echo $Row['Id'];?>" onClick="return confirm('Are you sure you want to delete this selected file ?')">Delete</a> 

代碼:

<?php 

// Including the database connection file 
include_once 'Ad_updbconnect.php'; 

// Getting Id of the data from url 
$Id = $_GET['Id']; 

$Del = glob('uploads/*'); // Get all file names 
foreach ($Del as $File) { 
    if (is_file($File)) { 
     unlink($File); // delete file ! 
    } 
} 

// Deleting the row from table 
$Result = mysqli_query ($Con, "DELETE FROM ibgsec_uploads WHERE Id=$Id"); 
$Row = mysqli_fetch_array($Result); 

// Redirecting to the display page. 
header("location: Ad_uploads.php"); 
?> 

這是留給我工作的唯一的東西,我試過很多方法來正確地執行命令,但它不起作用。

回答

0

您的代碼顯式刪除上傳中的每個文件。你期望什麼?

$Del = glob('uploads/*'); // Get all file names 
foreach ($Del as $File) { 
    if (is_file($File)) { 
     unlink($File); // delete file ! 
    } 
} 

$Id一個文件名?似乎有風險,但你仍然想嘗試與$File進行比較。比較東西$File否則你只是刪除它們。

也被命名您的PARAM中的鏈接$Id<a href="Ad_delete.php?$Id=<?php echo $Row['Id'];?>"但獲取它作爲Id$Id = $_GET['Id'];。然後,通過將其包含在數據庫查詢中,打開自己的SQL注入。

相關問題