Question

I've managed to get a photo to be uploaded to the server and then written to the database thanks to quite a bit of help but I need to be able to upload three files and each needs to be written to the database accordingly. So at the moment only photo1 is being uploaded and written, I'd like to create another form input for photo2 and 3 and have them also written and uploaded. Sorry I'm almost a complete beginner with php, any help will be hugely appreciated! thanks in advance.

<?php


    session_start();

    include_once('../php/connection.php');

    if (isset($_SESSION['logged_in'])) {

        if (isset($_POST['title'], $_POST['content'], $_FILES['photo1'])) {

            $title    = $_POST['title'];
            $content  = nl2br($_POST['content']);
            $name     = $_FILES['photo1']['name'];
            $tmp_name = $_FILES['photo1,']['tmp_name'];

            $target = '../lifestyle/'.$name;

            if (move_uploaded_file($tmp_name, $target)) {

                $stmt = $pdo->prepare('INSERT INTO article (article_title, article_content, photo_1, photo_2) VALUES (?,?,?,?)');
                $stmt->execute(array($title,$content,$name,));
                header('Location: index.php');
                exit();

            }

        }

        ?>



<form action="add.php" method="post" autocomplete="off" enctype="multipart/form-data"/>
    <<input type="text" name="title" id="title"/>
    <textarea name="content"></textarea></dt>
    <input type="file" name="photo1" >
    <input type="submit" id="add article"/>
    </form>
Was it helpful?

Solution

Use attribute multiple and make some sort of array of the name:

<input type="file" name="photo[]" multiple >

OR

<input type="file" name="photo[]">
<input type="file" name="photo[]">
<input type="file" name="photo[]">

http://php.net/manual/en/features.file-upload.multiple.php

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top