2017-05-04 94 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注入。

相关问题