-1

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

  • Possible duplicate of [Multiple Image Upload PHP form with one input](https://stackoverflow.com/questions/24895170/multiple-image-upload-php-form-with-one-input) – Vishnu Bhadoriya Nov 25 '19 at 13:05
  • We can suggest you, not can code for you. Add `multiple` attribute to the input field, name of input field should be like `photo[]` and handle upload using a loop. – Lovepreet Singh Nov 25 '19 at 13:07
  • _“please help me”_ - please go read [ask]. You should rather not be asking such overly broad “how can I do X” questions here - we expect you to actually put in some initial effort, do your own research, and try something. – 04FS Nov 25 '19 at 13:10

1 Answers1

0

HTML

    <div class="col-sm-5">
                    <input type="file" id="photo" name="photo" multiple>
                  </div>

EDITED

$images = explode(',',$product['photo']));
foreach($images as $val){
    <img src="<?php echo (!empty($val)) ? 'images/'.$val : 'images/noimage.jpg'; ?>" width="100%" class="zoom" data-magnify-src="images/large-<?php echo $val; ?>">
}

In Database take text datatype for photo

PHP

    if(isset($_POST['add'])){
    $name = $_POST['name'];
    $slug = slugify($name);
    $category = $_POST['category'];
    $price = $_POST['price'];
    $description = $_POST['description'];

    $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(isset($_FILES['photo']['name'][0]) && $_FILES['photo']['size'][0] != 0 && $_FILES['photo']['error'][0] == 0) 
        {
         $filesCount = count($_FILES['photo']['name']);
         for($i = 0; $i < $filesCount; $i++) { 
            $ext = pathinfo($_FILES['photo']['name'][$i], PATHINFO_EXTENSION);

            $new_filename = $slug.'_'date('Y-m-d h:i:s').'.'.$ext;
            move_uploaded_file($_FILES['photo']['tmp_name'][$i], '../images/'.$new_filename);   
            $allfiles[] = $new_filename;
         }
         $uploaded_img = implode(',',$allfiles);
        }
        else{
            $uploaded_img = '';
        }

        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'=>$uploaded_img]);
            $_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');
Vaibhavi S.
  • 1,083
  • 7
  • 20