我正在寻找一种在单击链接时使用php执行函数的方法。我的意思是在单击此链接时删除一个项目。我像这样设置了我的代码:
$files = glob("upload/*.*");
if(count($files) == 0){
echo "No files present";
} else {
foreach ( $files as $file ) {
$array = explode("/", $file);
//echo $array[1] . "<br>";
echo "<tr>";
echo "<td>" . $array[1] . "</td>";
echo '<td><img height="70" width="auto" src=" ' . $file . '" ></td>';
echo '<td><a href="">Delete item</a></td>';
echo "</tr>";
}
}
在主脚本中:
$files = glob("upload/*.*");
if(!count($files)){
echo "No files present";
} else {
foreach ($files as $file) {
$array = explode("/", $file);
echo "<tr>";
echo "<td>" . $array[1] . "</td>";
echo '<td><img height="70" width="auto" src=" ' . $file . '" ></td>';
echo '<td><a href="delete.php?file='.rawurlencode($file).'">Delete item</a></td>';
echo "</tr>";
}
}
在删除脚本中:
if (isset($_GET['file']) && $file = rawurldecode($_GET['file'])) {
if (file_exists('upload/' . $file))
unlink($file);
}
您可能需要传入另一个$_GET
参数(秘密令牌)以增加安全性 - 否则它会将您的上传目录打开到永久威胁。您可以发送随机sha1()
哈希或系统定义的配置值,并让删除脚本需要 40 个字符的字符串。