[英]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的unlink功能:
<?php
unlink('./' . $img_path . '/' . $image);
unlink('./thumbs/' . $img_path . '/' . $image);
?>
在庫中創建錨標記,文件名為標識屬性。 例如。
<?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);
}
在錨標記內創建一個刪除按鈕,用於將頁面定向到刪除腳本,傳入圖像的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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.