I am new to php programming I have codes that allow user to upload image in my website Here is html part...
<div class="col-sm-5">
<input type="file" id="photo" name="photo">
</div>
Here is php part
if(isset($_POST['add'])){
$name = $_POST['name'];
$slug = slugify($name);
$category = $_POST['category'];
$price = $_POST['price'];
$description = $_POST['description'];
$filename = $_FILES['photo']['name'];
$conn = $pdo->open();
$stmt = $conn->prepare("SELECT *, COUNT(*) AS numrows FROM products WHERE slug=:slug");
$stmt->execute(['slug'=>$slug]);
$row = $stmt->fetch();
if($row['numrows'] > 0){
$_SESSION['error'] = 'Product already exist';
}
else{
if(!empty($filename)){
$ext = pathinfo($filename, PATHINFO_EXTENSION);
$new_filename = $slug.'.'.$ext;
move_uploaded_file($_FILES['photo']['tmp_name'], '../images/'.$new_filename);
}
else{
$new_filename = '';
}
try{
$stmt = $conn->prepare("INSERT INTO products (category_id, name, description, slug, price, photo) VALUES (:category, :name, :description, :slug, :price, :photo)");
$stmt->execute(['category'=>$category, 'name'=>$name, 'description'=>$description, 'slug'=>$slug, 'price'=>$price, 'photo'=>$new_filename]);
$_SESSION['success'] = 'User added successfully';
}
catch(PDOException $e){
$_SESSION['error'] = $e->getMessage();
}
}
$pdo->close();
}
else{
$_SESSION['error'] = 'Fill up product form first';
}
header('location: products.php');
Then...on displaying the image
<img src="<?php echo (!empty($product['photo'])) ? 'images/'.$product['photo'] : 'images/noimage.jpg'; ?>" width="100%" class="zoom" data-magnify-src="images/large-<?php echo $product['photo']; ?>">
The codes work fine except it allow user to upload only one image...How can i change so that user can upload four images.....please help me.Thanks