add.php(When user click add photo)
<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']);
}
The Problem -- What you should do:
You basically have to populate the
SESSION variable
like this:You're meant to address each element therefore, like this:
$_SESSION["auctionImages"][$n];
$n
is the numbered index value for a particular element in the array. Therefore, if$n
is 0, the array would return "IMG_29.29.JPG" and if the$n
is 1, the array would return "IMG_2924.JPG".However, you are populating the array like this:
If you dump this array, it will give you:
Which is not the behaviour you require.
Solution
This is more shorter, cleaner and neater.
Also, you can use the alternative array syntax which is
[
and]
. So, you can declare arrays using$var = [];
which is shorter than$var = array();
.Firstly, the variable
$a
is the text to be searched in the array.This is the second part of the code. This is all you need to have.
Also, make sure you have started the session by invoking
session_start()
in the top of the file if you haven't done yet.A few comments
$camelCase
. Therefore, it would be better to rename$filename
to$fileName
.strict comparison
which is!==
.$a
does not make sense. Something like$searchString
would be really meaningful and the code will self-document your code.Links
is_array
- Returns TRUE if the passed identifier is an array, otherwise returns FALSE.Let's now solve the problem with the full code you have given me. Let's start with
delete.php
:Now, let's fix your
UploadImages.php
file:You have a problem here
Variable
$auctionImage
itself an array so need not to assign as an array again inSESSION
variable. Make it asIt works fine for me.
below is the code I worked.
To use session variables, please add session_start() at the begin of your files, otherwise they aren't used. Secondly you are adding an array into a next array. so you have to use
$_SESSION["auctionImages"] = $auctionImage;
or$key=array_search($a[0],$_SESSION['auctionImages']);
Further debugging can be done by print_r($_SESSION); so you can track the contents of this array