0

Below code uploads images one by one. I want to upload multiple images!

    <?php
//config
        require("./configuration.php");

            $id = $_GET['id'];
            $q = mysql_query("SELECT * FROM books WHERE id='$id'") or die(mysql_error());
            $row = mysql_fetch_array($q);
            $name = $row['name'];
            echo '<div class="center-top"> NAME: <strong style="font-size:22px;">'.$name.'</strong>  </div>';
        //begin upload
        if(isset($_POST['submit'])) 
        {

            $title = htmlspecialchars($_POST['title']);
                $path = "./upload/images/";
                $name_pic = $_FILES['file']['name'];
                $ext = strtolower(substr(strrchr($name_pic, "."), 1));
                $allow = array("jpg", "jpeg", "JPG", "JPEG", "png", "gif");
               $uptype = ($_FILES['file']['tmp_name']);
                if (in_array($ext, $allow))
                {
                       $rand = rand(0,10000);
                       $md5  = md5($rand);
                       $new_file_name = "{$md5}.{$ext}";
                       $move_file = move_uploaded_file($_FILES['file']['tmp_name'], $path.$new_file_name);
                       if($move_file) {
                            mysql_query("INSERT INTO images (id, link, title, bid)
                            VALUES (NULL, '$new_file_name', '$title', '$id')") or die (mysql_error());
                            echo "<div class='yes'>succesfully added!</div>";   
        }}}

        //form
        echo '
        <form method="post" action="images.php?id='.$id.'"  enctype="multipart/form-data"> 
        <br />title: <br /><input type="text" name="title" value="'.$name.'"/><br />
        <br /><br/><input name="file" type="file" />
        <br /><input type="submit" name="submit" value="UPLOAD" /><br /><br />
        </form> 
        ';
        ?>

With this code delete an image:

<?php
$id = $_GET['id']; 
$result = mysql_query("DELETE FROM imagesWHERE `id`='$id'");
echo "<div class='yes'>successfully deleted picture!</div>";
?>
6
  • 1
    Possible duplicate of PHP - Upload multiple images Commented Jan 25, 2016 at 16:35
  • 1
    You are vulnerable to sql injection attacks. Enjoy having your server pwn3d. Commented Jan 25, 2016 at 16:37
  • @MarcB: Why do you have to be so rude about it? Give some positive feedback instead of this... Commented Jan 25, 2016 at 16:38
  • I myself upload images I'm admin, no other can access! @MarcB can you tell me what to do to jump over sql injection attack? Commented Jan 25, 2016 at 16:39
  • why does no one ever follow that link? it explains the problem and offers solutions for all kinds of languages... Commented Jan 25, 2016 at 16:43

1 Answer 1

0

First of all, you need to declare <input name="file" type="file"/> as <input name="file[]" type="file" multiple/> Then, find total nos of images selected. Put it in for loop and start storing in your upload->images folder and save image path in images column.

<?php

//config
require("./configuration.php");

$id = $_GET['id'];
$q = mysql_query("SELECT * FROM books WHERE id='$id'") or die(mysql_error());
$row = mysql_fetch_array($q);
$name = $row['name'];
echo '<div class="center-top"> NAME: <strong style="font-size:22px;">'.$name.'</strong>  </div>';
if(isset($_POST['submit'])) 
{
    $title = htmlspecialchars($_POST['title']);
    $path = "./upload/images/";
    $TotalImage = count($_FILES['file']['name']);
    for($i=0;$i>$TotalImage;$i++)
    { 

        $image_name = $_FILES['file']['name'][$i];
        $new_file_name = $path.$image_name;
        if(move_uploaded_file($_FILES['file']['tmp_name'][$i],$path.$image_name))
        {
            mysql_query("INSERT INTO images (id, link, title, bid) VALUES (NULL, '$new_file_name', '$title', '$id')") or die (mysql_error());
            echo "<div class='yes'>succesfully added!</div>";   
        }
    }
}

//form
echo '
<form method="post" action="images.php?id='.$id.'"  enctype="multipart/form-data"> 
<br />title: <br /><input type="text" name="title" value="'.$name.'"/><br />
<br /><br/><input name="file[]" type="file" multiple/>
<br /><input type="submit" name="submit" value="UPLOAD" /><br /><br />
</form> 
';
?>
Sign up to request clarification or add additional context in comments.

2 Comments

Actually doesn't work! No errors but not inserts into database
You have used the same code which i wrote as an answer. Because, in my desktop it's working. Lot of differences is there in your or my code. So, check it properly. It should work. @yaman. Not only input file changed but, also the execution page also changed. Check it once.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.