Skip to Content
PHPCODE
Multiple file upload with remove option in PHP
php code / September 21, 2021

Multiple photographs can be uploaded at once, which cuts down on the time it takes to upload a large number of images to the server. It also allows you to upload numerous photographs to the server at once rather than one at a time. The web application’s dynamic gallery management part benefits greatly from the ability to add multiple images. It’s especially useful in the product management section, where many photos for each product must be uploaded.

PHP allows you to upload numerous photos with a single click. Not only can you submit images to the server, but you can also upload data alongside images using PHP and MySQL. In this lesson, we’ll show you how to use PHP and MySQL to upload numerous photos and manage data (view, edit, and remove).

We’ll use PHP and MySQL to create a gallery management system with multiple images in the example script.

Get information about the gallery from the database and put it on the webpage.
Add form data to the database and upload several photos to the server.
View a gallery featuring a variety of photos.
Multiple photos can be edited and updated.
Remove the gallery and various photographs from your computer.

Take a look at the file structure of the multiple picture upload management script before getting started.

multiple_image_upload_crud/
├── index.php
├── view.php
├── addEdit.php
├── postAction.php
├── DB.class.php
├── uploads/
│   └── images/
├── js/
│   └── jquery.min.js
├── bootstrap/
│   └── bootstrap.min.css
├── css/
│   └── style.css
└── images/

Make a database table.

Two tables in the database are required to store the gallery and picture file information.

1. Please read the following: In the MySQL database, SQL creates a gallery table with some basic fields.

CREATE TABLE `gallery` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
`status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '1=Active | 0=Inactive',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

2. Please read the following: In the MySQL database, SQL constructs a gallery images table with a parent gallery identification field (gallery id).

CREATE TABLE `gallery_images` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`gallery_id` int(11) NOT NULL,
`file_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`uploaded_on` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Database Type (DB.class.php)

The DB class is in charge of all database operations (connect, insert, update, and delete). According to your MySQL database credentials, specify the database host ($dbHost), username ($dbUsername), password ($dbPassword), and name ($dbName).

__construct() — Use PHP and the MySQLi Extension to connect to a database.

getRows() is a function that returns the number of rows in a table.

Fetch a gallery of photographs from the database depending on the criteria you specify.

If only a single row is requested, the gallery data will be returned, together with all of the relevant images. Otherwise, using the gallery data, only one image is returned.

getImgRow() — Retrieves image data from a database based on a given ID.

Insert gallery data into the database with insert().
Insert gallery images into the database with insertImage().
update() – Inserts data from the gallery into the database.
Delete gallery data from the database with delete().
deleteImage() — Removes a specific gallery’s image from the database.

<?php 
/* 
* DB Class 
* This class is used for database related (connect, insert, update, and delete) operations 
* @author CodexWorld.com 
* @url http://www.codexworld.com 
* @license http://www.codexworld.com/license 
*/ 
class DB{ 
private $dbHost = "localhost"; 
private $dbUsername = "root"; 
private $dbPassword = "root"; 
private $dbName = "codexworld"; 
private $galleryTbl = "gallery"; 
private $imgTbl = "gallery_images"; 
public function __construct(){ 
if(!isset($this->db)){ 
// Connect to the database 
$conn = new mysqli($this->dbHost, $this->dbUsername, $this->dbPassword, $this->dbName); 
if($conn->connect_error){ 
die("Failed to connect with MySQL: " . $conn->connect_error); 
}else{ 
$this->db = $conn; 
} 
} 
} 
/* 
* Returns rows from the database based on the conditions 
* @param string name of the table 
* @param array select, where, order_by, limit and return_type conditions 
*/ 
public function getRows($conditions = array()){ 
$sql = 'SELECT '; 
$sql .= '*, (SELECT file_name FROM '.$this->imgTbl.' WHERE gallery_id = '.$this->galleryTbl.'.id ORDER BY id DESC LIMIT 1) as default_image'; 
$sql .= ' FROM '.$this->galleryTbl; 
if(array_key_exists("where",$conditions)){ 
$sql .= ' WHERE '; 
$i = 0; 
foreach($conditions['where'] as $key => $value){ 
$pre = ($i > 0)?' AND ':''; 
$sql .= $pre.$key." = '".$value."'"; 
$i++; 
} 
} 
if(array_key_exists("order_by",$conditions)){ 
$sql .= ' ORDER BY '.$conditions['order_by']; 
}else{ 
$sql .= ' ORDER BY id DESC '; 
} 
if(array_key_exists("start",$conditions) && array_key_exists("limit",$conditions)){ 
$sql .= ' LIMIT '.$conditions['start'].','.$conditions['limit']; 
}elseif(!array_key_exists("start",$conditions) && array_key_exists("limit",$conditions)){ 
$sql .= ' LIMIT '.$conditions['limit']; 
} 
$result = $this->db->query($sql); 
if(array_key_exists("return_type",$conditions) && $conditions['return_type'] != 'all'){ 
switch($conditions['return_type']){ 
case 'count': 
$data = $result->num_rows; 
break; 
case 'single': 
$data = $result->fetch_assoc(); 
if(!empty($data)){ 
$sql = 'SELECT * FROM '.$this->imgTbl.' WHERE gallery_id = '.$data['id']; 
$result = $this->db->query($sql); 
$imgData = array(); 
if($result->num_rows > 0){ 
while($row = $result->fetch_assoc()){ 
$imgData[] = $row; 
} 
} 
$data['images'] = $imgData; 
} 
break; 
default: 
$data = ''; 
} 
}else{ 
if($result->num_rows > 0){ 
while($row = $result->fetch_assoc()){ 
$data[] = $row; 
} 
} 
} 
return !empty($data)?$data:false; 
} 
public function getImgRow($id){ 
$sql = 'SELECT * FROM '.$this->imgTbl.' WHERE id = '.$id; 
$result = $this->db->query($sql); 
return ($result->num_rows > 0)?$result->fetch_assoc():false; 
} 
/* 
* Insert data into the database 
* @param string name of the table 
* @param array the data for inserting into the table 
*/ 
public function insert($data){ 
if(!empty($data) && is_array($data)){ 
$columns = ''; 
$values = ''; 
$i = 0; 
if(!array_key_exists('created',$data)){ 
$data['created'] = date("Y-m-d H:i:s"); 
} 
if(!array_key_exists('modified',$data)){ 
$data['modified'] = date("Y-m-d H:i:s"); 
} 
foreach($data as $key=>$val){ 
$pre = ($i > 0)?', ':''; 
$columns .= $pre.$key; 
$values .= $pre."'".$this->db->real_escape_string($val)."'"; 
$i++; 
} 
$query = "INSERT INTO ".$this->galleryTbl." (".$columns.") VALUES (".$values.")"; 
$insert = $this->db->query($query); 
return $insert?$this->db->insert_id:false; 
}else{ 
return false; 
} 
} 
public function insertImage($data){ 
if(!empty($data) && is_array($data)){ 
$columns = ''; 
$values = ''; 
$i = 0; 
if(!array_key_exists('uploaded_on',$data)){ 
$data['uploaded_on'] = date("Y-m-d H:i:s"); 
} 
foreach($data as $key=>$val){ 
$pre = ($i > 0)?', ':''; 
$columns .= $pre.$key; 
$values .= $pre."'".$this->db->real_escape_string($val)."'"; 
$i++; 
} 
$query = "INSERT INTO ".$this->imgTbl." (".$columns.") VALUES (".$values.")"; 
$insert = $this->db->query($query); 
return $insert?$this->db->insert_id:false; 
}else{ 
return false; 
} 
} 
/* 
* Update data into the database 
* @param string name of the table 
* @param array the data for updating into the table 
* @param array where condition on updating data 
*/ 
public function update($data, $conditions){ 
if(!empty($data) && is_array($data)){ 
$colvalSet = ''; 
$whereSql = ''; 
$i = 0; 
if(!array_key_exists('modified',$data)){ 
$data['modified'] = date("Y-m-d H:i:s"); 
} 
foreach($data as $key=>$val){ 
$pre = ($i > 0)?', ':''; 
$colvalSet .= $pre.$key."='".$this->db->real_escape_string($val)."'"; 
$i++; 
} 
if(!empty($conditions)&& is_array($conditions)){ 
$whereSql .= ' WHERE '; 
$i = 0; 
foreach($conditions as $key => $value){ 
$pre = ($i > 0)?' AND ':''; 
$whereSql .= $pre.$key." = '".$value."'"; 
$i++; 
} 
} 
$query = "UPDATE ".$this->galleryTbl." SET ".$colvalSet.$whereSql; 
$update = $this->db->query($query); 
return $update?$this->db->affected_rows:false; 
}else{ 
return false; 
} 
} 
/* 
* Delete data from the database 
* @param string name of the table 
* @param array where condition on deleting data 
*/ 
public function delete($conditions){ 
$whereSql = ''; 
if(!empty($conditions)&& is_array($conditions)){ 
$whereSql .= ' WHERE '; 
$i = 0; 
foreach($conditions as $key => $value){ 
$pre = ($i > 0)?' AND ':''; 
$whereSql .= $pre.$key." = '".$value."'"; 
$i++; 
} 
} 
$query = "DELETE FROM ".$this->galleryTbl.$whereSql; 
$delete = $this->db->query($query); 
return $delete?true:false; 
} 
public function deleteImage($conditions){ 
$whereSql = ''; 
if(!empty($conditions)&& is_array($conditions)){ 
$whereSql .= ' WHERE '; 
$i = 0; 
foreach($conditions as $key => $value){ 
$pre = ($i > 0)?' AND ':''; 
$whereSql .= $pre.$key." = '".$value."'"; 
$i++; 
} 
} 
$query = "DELETE FROM ".$this->imgTbl.$whereSql; 
$delete = $this->db->query($query); 
return $delete?true:false; 
} 
}

The Bootstrap Library is a collection of HTML5 and CSS3

The table, list, form fields, and links are all created using the Bootstrap 4 library. As a result, include the Bootstrap library’s CSS file. Remove Bootstrap from the include list if you don’t want to utilise it.

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">

List of Galleries (index.php)

All gallery data is initially downloaded from the database and displayed in a tabular view with buttons to view, add, edit, and delete.

The View button allows you to see the gallery’s photographs.
The Add button allows you to upload gallery information as well as multiple photographs.

The Edit link allows you to make changes to the gallery’s information and upload/delete photographs.
Using the Delete link, you can remove galleries and photographs from the database.
The gallery’s visibility can be controlled using the Status badge (Active/Inactive).

<?php 
// Start session 
session_start(); 
// Include and initialize DB class 
require_once 'DB.class.php'; 
$db = new DB(); 
// Fetch the gallery data 
$images = $db->getRows(); 
// Get session data 
$sessData = !empty($_SESSION['sessData'])?$_SESSION['sessData']:''; 
// Get status message from session 
if(!empty($sessData['status']['msg'])){ 
$statusMsg = $sessData['status']['msg']; 
$statusMsgType = $sessData['status']['type']; 
unset($_SESSION['sessData']['status']); 
} 
?>
<!-- Display status message -->
<?php if(!empty($statusMsg)){ ?>
<div class="col-xs-12">
<div class="alert alert-<?php echo $statusMsgType; ?>"><?php echo $statusMsg; ?></div>
</div>
<?php } ?>
<div class="row">
<div class="col-md-12 head">
<h5>Images</h5>
<!-- Add link -->
<div class="float-right">
<a href="addEdit.php" class="btn btn-success"><i class="plus"></i> New Gallery</a>
</div>
</div>
<!-- List the images -->
<table class="table table-striped table-bordered">
<thead class="thead-dark">
<tr>
<th width="5%">#</th>
<th width="10%"></th>
<th width="40%">Title</th>
<th width="19%">Created</th>
<th width="8%">Status</th>
<th width="18%">Action</th>
</tr>
</thead>
<tbody>
<?php 
if(!empty($images)){ $i=0; 
foreach($images as $row){ $i++; 
$defaultImage = !empty($row['default_image'])?'<img src="uploads/images/'.$row['default_image'].'" alt="" />':''; 
$statusLink = ($row['status'] == 1)?'postAction.php?action_type=block&id='.$row['id']:'postAction.php?action_type=unblock&id='.$row['id']; 
$statusTooltip = ($row['status'] == 1)?'Click to Inactive':'Click to Active'; 
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $defaultImage; ?></td>
<td><?php echo $row['title']; ?></td>
<td><?php echo $row['created']; ?></td>
<td><a href="<?php echo $statusLink; ?>" title="<?php echo $statusTooltip; ?>"><span class="badge <?php echo ($row['status'] == 1)?'badge-success':'badge-danger'; ?>"><?php echo ($row['status'] == 1)?'Active':'Inactive'; ?></span></a></td>
<td>
<a href="view.php?id=<?php echo $row['id']; ?>" class="btn btn-primary">view</a>
<a href="addEdit.php?id=<?php echo $row['id']; ?>" class="btn btn-warning">edit</a>
<a href="postAction.php?action_type=delete&id=<?php echo $row['id']; ?>" class="btn btn-danger" onclick="return confirm('Are you sure to delete data?')?true:false;">delete</a>
</td>
</tr>
<?php } }else{ ?>
<tr><td colspan="6">No gallery found...</td></tr>
<?php } ?>
</tbody>
</table>
</div>

Using AJAX, delete gallery images
The picture deletes capability is implemented into the view.php and addEdit.php files. Ajax is used to delete photographs from the gallery, and jQuery is used to do so.

Where the gallery picture removal feature is utilised, include the jQuery library.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

The deleteImage() function sends an AJAX request to the gallery to delete an image.

POST the file ID to the file postAction.php.
The element is removed from the web page and the specified picture is erased from the database.

<script>
function deleteImage(id){
var result = confirm("Are you sure to delete?");
if(result){
$.post( "postAction.php", {action_type:"img_delete",id:id}, function(resp) {
if(resp == 'ok'){
$('#imgb_'+id).remove();
alert('The image has been removed from the gallery');
}else{
alert('Some problem occurred, please try again.');
}
});
}
}
</script>

Multiple Images with Gallery Information (view.php)
The gallery information is displayed in the view.php file.

The Delete link appears next to all of the photographs in the gallery.
When the Delete button is pressed, the deleteImage() method is called, and the corresponding image is removed from the gallery using PHP and jQuery Ajax.

<?php 
if(empty($_GET['id'])){ 
header("Location: manage.php"); 
} 
// Include and initialize DB class 
require_once 'DB.class.php'; 
$db = new DB(); 
$conditions['where'] = array( 
'id' => $_GET['id'], 
); 
$conditions['return_type'] = 'single'; 
$galData = $db->getRows($conditions); 
?>
<div class="row">
<div class="col-md-12">
<h5><?php echo !empty($galData['title'])?$galData['title']:''; ?></h5>
<?php if(!empty($galData['images'])){ ?>
<div class="gallery-img">
<?php foreach($galData['images'] as $imgRow){ ?>
<div class="img-box" id="imgb_<?php echo $imgRow['id']; ?>">
<img src="uploads/images/<?php echo $imgRow['file_name']; ?>">
<a href="javascript:void(0);" class="badge badge-danger" onclick="deleteImage('<?php echo $imgRow['id']; ?>')">delete</a>
</div>
<?php } ?>
</div>
<?php } ?>
</div>
<a href="index.php" class="btn btn-primary">Back to List</a>
</div>

Form for uploading multiple images and adding/editing data (addEdit.php)
The HTML form that allows the user to pick several image files and enter the gallery name is stored in the addEdit.php file.

The form data is initially sent to the PHP script (postAction.php), which uploads numerous photos and inserts the form data into the database.
The existing gallery data will be pulled from the database if the ID parameter is present in the URL.
Images are listed under the file upload form and data is pre-filled in the input areas.
The information is sent to the PHP script (postAction.php), which updates the database and uploads fresh photos to the server.

<?php 
// Start session 
session_start(); 
$postData = $galData = array(); 
// Get session data 
$sessData = !empty($_SESSION['sessData'])?$_SESSION['sessData']:''; 
// Get status message from session 
if(!empty($sessData['status']['msg'])){ 
$statusMsg = $sessData['status']['msg']; 
$statusMsgType = $sessData['status']['type']; 
unset($_SESSION['sessData']['status']); 
} 
// Get posted data from session 
if(!empty($sessData['postData'])){ 
$postData = $sessData['postData']; 
unset($_SESSION['sessData']['postData']); 
} 
// Get gallery data 
if(!empty($_GET['id'])){ 
// Include and initialize DB class 
require_once 'DB.class.php'; 
$db = new DB(); 
$conditions['where'] = array( 
'id' => $_GET['id'], 
); 
$conditions['return_type'] = 'single'; 
$galData = $db->getRows($conditions); 
} 
// Pre-filled data 
$galData = !empty($postData)?$postData:$galData; 
// Define action 
$actionLabel = !empty($_GET['id'])?'Edit':'Add'; 
?>
<div class="container">
<h1><?php echo $actionLabel; ?> Gallery</h1>
<hr>
<!-- Display status message -->
<?php if(!empty($statusMsg)){ ?>
<div class="col-xs-12">
<div class="alert alert-<?php echo $statusMsgType; ?>"><?php echo $statusMsg; ?></div>
</div>
<?php } ?>
<div class="row">
<div class="col-md-6">
<form method="post" action="postAction.php" enctype="multipart/form-data">
<div class="form-group">
<label>Images:</label>
<input type="file" name="images[]" class="form-control" multiple>
<?php if(!empty($galData['images'])){ ?>
<div class="gallery-img">
<?php foreach($galData['images'] as $imgRow){ ?>
<div class="img-box" id="imgb_<?php echo $imgRow['id']; ?>">
<img src="uploads/images/<?php echo $imgRow['file_name']; ?>">
<a href="javascript:void(0);" class="badge badge-danger" onclick="deleteImage('<?php echo $imgRow['id']; ?>')">delete</a>
</div>
<?php } ?>
</div>
<?php } ?>
</div>
<div class="form-group">
<label>Title:</label>
<input type="text" name="title" class="form-control" placeholder="Enter title" value="<?php echo !empty($galData['title'])?$galData['title']:''; ?>" >
</div>
<a href="index.php" class="btn btn-secondary">Back</a>
<input type="hidden" name="id" value="<?php echo !empty($galData['id'])?$galData['id']:''; ?>">
<input type="submit" name="imgSubmit" class="btn btn-success" value="SUBMIT">
</form>
</div>
</div>
</div>

Add, Edit, and Delete Records, as well as Upload Multiple Images (postAction.php)
Using PHP and MySQL, this file supports multiple file uploads, as well as add, edit, and delete actions.

Submit: 1. Add / Edit Form:

The data from the submitted form is checked for empty fields.
The DB class’s insert() and update() methods are used to add and update gallery data in the database.
Using PHP’s pathinfo() function, check and validate the file extension.
Using PHP’s move uploaded file() method, upload numerous photos to the server.
Using the insertImage() function of the DB class, insert the uploaded file names and gallery ID into the database.

Gallery is inactive (action type => block):

In the database, update and set the gallery status to 0.

The DB class’s update() method is used to update the database’s status field value.

3. Unblock (action type => unblock) the Gallery:

In the database, update and set the gallery status to 1.

The DB class’s update() method is used to update the database’s status field value.

4. Gallery Delete (action type => delete):

Delete the data for the gallery and photos from the database.

Remove photos from the server’s directory.

To erase gallery and image data from the database, utilise the DB class’s delete() and deleteImage() methods.

Image deletion (action type = img delete):

Delete the database’s picture data.
Remove the picture file from the server’s directory.
The DB class’s deleteImage() method is used to remove picture data from the database.
Following data manipulation, the status is saved in PHP SESSION and the user is redirected to the appropriate page.

<?php 
// Start session 
session_start(); 
// Include and initialize DB class 
require_once 'DB.class.php'; 
$db = new DB(); 
// File upload path 
$uploadDir = "uploads/images/"; 
// Allow file formats 
$allowTypes = array('jpg','png','jpeg','gif'); 
// Set default redirect url 
$redirectURL = 'index.php'; 
$statusMsg = $errorMsg = ''; 
$sessData = array(); 
$statusType = 'danger'; 
if(isset($_POST['imgSubmit'])){ 
// Set redirect url 
$redirectURL = 'addEdit.php'; 
// Get submitted data 
$title = $_POST['title']; 
$id = $_POST['id']; 
// Submitted user data 
$galData = array( 
'title' => $title 
); 
// Store submitted data into session 
$sessData['postData'] = $galData; 
$sessData['postData']['id'] = $id; 
// ID query string 
$idStr = !empty($id)?'?id='.$id:''; 
if(empty($title)){ 
$error = '<br/>Enter the gallery title.'; 
} 
if(!empty($error)){ 
$statusMsg = 'Please fill all the mandatory fields.'.$error; 
}else{ 
if(!empty($id)){ 
// Update data 
$condition = array('id' => $id); 
$update = $db->update($galData, $condition); 
$galleryID = $id; 
}else{ 
// Insert data 
$insert = $db->insert($galData); 
$galleryID = $insert; 
} 
$fileImages = array_filter($_FILES['images']['name']); 
if(!empty($galleryID)){ 
if(!empty($fileImages)){ 
foreach($fileImages as $key=>$val){ 
// File upload path 
$fileName = $galleryID.'_'.basename($fileImages[$key]); 
$targetFilePath = $uploadDir . $fileName; 
// Check whether file type is valid 
$fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION); 
if(in_array($fileType, $allowTypes)){ 
// Upload file to server 
if(move_uploaded_file($_FILES["images"]["tmp_name"][$key], $targetFilePath)){ 
// Image db insert 
$imgData = array( 
'gallery_id' => $galleryID, 
'file_name' => $fileName 
); 
$insert = $db->insertImage($imgData); 
}else{ 
$errorUpload .= $fileImages[$key].' | '; 
} 
}else{ 
$errorUploadType .= $fileImages[$key].' | '; 
} 
} 
$errorUpload = !empty($errorUpload)?'Upload Error: '.trim($errorUpload, ' | '):''; 
$errorUploadType = !empty($errorUploadType)?'File Type Error: '.trim($errorUploadType, ' | '):''; 
$errorMsg = !empty($errorUpload)?'<br/>'.$errorUpload.'<br/>'.$errorUploadType:'<br/>'.$errorUploadType; 
} 
$statusType = 'success'; 
$statusMsg = 'Gallery images has been uploaded successfully.'.$errorMsg; 
$sessData['postData'] = ''; 
$redirectURL = 'index.php'; 
}else{ 
$statusMsg = 'Some problem occurred, please try again.'; 
// Set redirect url 
$redirectURL .= $idStr; 
} 
} 
// Status message 
$sessData['status']['type'] = $statusType; 
$sessData['status']['msg'] = $statusMsg; 
}elseif(($_REQUEST['action_type'] == 'block') && !empty($_GET['id'])){ 
// Update data 
$galData = array('status' => 0); 
$condition = array('id' => $_GET['id']); 
$update = $db->update($galData, $condition); 
if($update){ 
$statusType = 'success'; 
$statusMsg = 'Gallery data has been blocked successfully.'; 
}else{ 
$statusMsg = 'Some problem occurred, please try again.'; 
} 
// Status message 
$sessData['status']['type'] = $statusType; 
$sessData['status']['msg'] = $statusMsg; 
}elseif(($_REQUEST['action_type'] == 'unblock') && !empty($_GET['id'])){ 
// Update data 
$galData = array('status' => 1); 
$condition = array('id' => $_GET['id']); 
$update = $db->update($galData, $condition); 
if($update){ 
$statusType = 'success'; 
$statusMsg = 'Gallery data has been activated successfully.'; 
}else{ 
$statusMsg = 'Some problem occurred, please try again.'; 
} 
// Status message 
$sessData['status']['type'] = $statusType; 
$sessData['status']['msg'] = $statusMsg; 
}elseif(($_REQUEST['action_type'] == 'delete') && !empty($_GET['id'])){ 
// Previous image files 
$conditions['where'] = array( 
'id' => $_GET['id'], 
); 
$conditions['return_type'] = 'single'; 
$prevData = $db->getRows($conditions); 
// Delete gallery data 
$condition = array('id' => $_GET['id']); 
$delete = $db->delete($condition); 
if($delete){ 
// Delete images data 
$condition = array('gallery_id' => $_GET['id']); 
$delete = $db->deleteImage($condition); 
// Remove files from server 
if(!empty($prevData['images'])){ 
foreach($prevData['images'] as $img){ 
@unlink($uploadDir.$img['file_name']); 
} 
} 
$statusType = 'success'; 
$statusMsg = 'Gallery has been deleted successfully.'; 
}else{ 
$statusMsg = 'Some problem occurred, please try again.'; 
} 
// Status message 
$sessData['status']['type'] = $statusType; 
$sessData['status']['msg'] = $statusMsg; 
}elseif(($_POST['action_type'] == 'img_delete') && !empty($_POST['id'])){ 
// Previous image data 
$prevData = $db->getImgRow($_POST['id']); 
// Delete gallery data 
$condition = array('id' => $_POST['id']); 
$delete = $db->deleteImage($condition); 
if($delete){ 
@unlink($uploadDir.$prevData['file_name']); 
$status = 'ok'; 
}else{ 
$status = 'err'; 
} 
echo $status;die; 
} 
// Store status into the session 
$_SESSION['sessData'] = $sessData; 
// Redirect the user 
header("Location: ".$redirectURL); 
exit(); 
?>

Note : 

This multiple picture upload management script is ideal for the web application’s dynamic data management part. This script can be used for a variety of things, including managing items with various photos using PHP and MySQL. You may simply improve the script’s functionality and use it in any data management part that requires multiple picture upload functionality.

PHPCODE © 2024