添加一个删除照片选项PHP


Add a delete photo option PHP

所以基本上我有一个PHP脚本,它从目录中用户的个人文件夹中获取所有图像,并将它们显示在他/她的图库页面上。现在我想添加一个删除功能,但我不知道该怎么做。任何帮助都会很棒!

<?php
$userid = $_GET['UID'];
$img_path = "./$userid";
chdir($img_path);
$images = glob('*.{jpg, jpeg, png, gif}', GLOB_BRACE);
foreach ($images as $image){
    if (file_exists("./thumbs/$img_path/{image}")){
        echo "<a href='$img_path/{$image}' rel='lightbox'><img src='./thumbs/$img_path/{$image}' alt='"{$image}'" /></a>";
    } else {
        echo "<a href='$img_path/{$image}' rel='lightbox'><img src='./$img_path/{$image}' width='200' height='150'   alt='"{$image}'" /></a>";
    }
}
?>

在库中创建一个以文件名为标识属性的锚标记。例如

<?php
$userid = $_GET['UID'];
$img_path = "./$userid";
chdir($img_path);
$images = glob('*.{jpg, jpeg, png, gif}', GLOB_BRACE);
foreach ($images as $image){
    if (file_exists("./thumbs/$img_path/{image}")){
        echo "<a href='$img_path/{$image}' rel='lightbox'><img src='./thumbs/$img_path/{$image}' alt='"{$image}'" /></a>";
        //add this line
        echo "<a href='$img_path/{$image}?delete=$image' rel='lightbox'>Delete</a>";    
    } else {
        echo "<a href='$img_path/{$image}' rel='lightbox'><img src='./$img_path/{$image}' width='200' height='150'   alt='"{$image}'" /></a>";
    }
}
//Updated and fixed the error, there was missing closing bracket here.
if(isset($_GET['delete'])) {
    $image = $_GET['delete'];
    unlink('./thumbs/'.$img_path.'/'.$image);
}

您可以使用php:的取消链接功能

<?php
unlink('./' . $img_path . '/' . $image);
unlink('./thumbs/' . $img_path . '/' . $image);
?>

在锚点标记中创建一个删除按钮,将页面指向删除脚本,并将图像的URL或图像的id作为参数传递。。。

<a href="http://example.com/delete.php?type=image&id=1">Delete this image</a>

在该脚本中,您可能需要更改文件/目录的权限:http://php.net/manual/en/function.chmod.php

删除(取消链接)文件之前:http://www.php.net/manual/en/function.unlink.php.

然后通过调用header()将用户送回原始页面:http://php.net/manual/en/function.header.php