add.php(當用戶點擊添加照片)如何基於會話變量的值刪除數據
<div class="col-lg-12">
<div class="form-group" id="image">
<label>Auction Image</label>
<div action="uploadImages.php" class="dropzone" id="uploadImageForm"></div>
<span class="help-block" id="image-error"></span>
</div>
</div>
<script>
$(function() {
Dropzone.options.uploadImageForm = false;
Dropzone.options.uploadImageForm = {
paramName: "file",
maxFilesize: 1,
acceptedFiles: 'image/*',
maxFiles: 5,
dictDefaultMessage: '<img src="images/icon_images.svg" width="100"/><br/><br/>Drop auction image here',
addRemoveLinks: true,
removedfile: function(file) {
var name = file.name;
$.ajax({
type: 'POST',
url: 'delete.php',
data: "id="+name,
dataType: 'html'
});
var _ref;
return (_ref = file.previewElement) != null ? _ref.parentNode.removeChild(file.previewElement) : void 0;
}
};
</script>
UploadImages.php
<?php
session_start();
require 'config/database.php';
if (!isset($_SESSION['user'])) {
exit;
}
else if (!empty($_FILES)) {
$auctionImage = array();
$size = getimagesize($_FILES['file']['tmp_name']);
if (!$size) {
header('Content-type: text/json');
header('Content-type: application/json');
echo json_encode(['error']);
exit;
}
else {
$n = 0;
$tempFile = $_FILES['file']['tmp_name'];
$imageName = uniqid() . '.' . pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
$targetPath = dirname(__FILE__) . '/images/uploads/';
$targetFile = $targetPath . $imageName;
$filename = $_FILES["file"]["name"];
move_uploaded_file($tempFile,$targetFile);
// isset id = insert gallery image into database
if (isset($_GET['id'])) {
$stmt = $db->prepare("INSERT INTO image (user_id, related_id, related_type, url) VALUES (:uid, :id, 'gallery', :url)");
$stmt->bindParam(':uid', $_SESSION['user']['id']);
$stmt->bindParam(':id', $_GET['id']);
$stmt->bindParam(':url', $imageName);
$stmt->execute();
}
else {
$auctionImage[] = $filename;
}
}
if (!empty($auctionImage)) {
// record uploaded image name, will store into session
// store uploaded image into session
//$_SESSION["auctionImages"] = array();
$_SESSION["auctionImages"][] = $auctionImage;
}
}
delete.php
<?php
$targetPath = dirname(__FILE__) . '/images/uploads/';
unlink($targetPath.$_POST['id']);
session_start();
$a = $_POST['id'];
$key=array_search($a,$_SESSION['auctionImages']);
if($key!==false){
unset($_SESSION['auctionImages'][$key]);
$_SESSION["auctionImages"] = array_values($_SESSION["auctionImages"]);
echo '<pre>'; print_r($_SESSION['auctionImages']);
}
具有u加上之前在session_start()? – Fiido93
@ChHong請看我的答案。我已經包含了一些可以幫助你的點。 –