0

I am having trouble getting the image files I wanted them to insert in my database I want the picture like the one I uploaded like this:

insert into('pic1.jpg');

insert into('pic2.jpg');

insert into('pic3.jpg');

not like this:

insert into(Array[0] => pic1.jpg, Array[1] => pic2.jpg, Array[2] => pic3.jpg);

so i can get their name only and insert in in my database im required to use foreach loop

  if(isset($_POST['upload'])){
      $upload[] = ($_FILES['images']['name']);
      print_r($upload);
   }


<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <form method="POST" enctype="multipart/form-data">        
        <input type="file" name="images[]" multiple="multiple">
        <input type="submit" name="upload" value="upload">
        </form>
    </body>
</html>

5 Answers 5

3

This is also one method:

foreach ($_FILES['images']['name'] as $f => $name) {
 $filename = $_FILES['images']['name'];
 $mysql_query1 = mysql_query("insert into table values('".$filename."')");
}
Sign up to request clarification or add additional context in comments.

Comments

2

Try this ...

   foreach ($_FILES['image']['tmp_name'] as $key => $val ) {

        $filename = $_FILES['image']['name'][$key];
        $filesize = $_FILES['image']['size'][$key];
        $filetempname = $_FILES['image']['tmp_name'][$key];

        $fileext = pathinfo($fileName, PATHINFO_EXTENSION);
        $fileext = strtolower($fileext);

        // here your insert query
    }

3 Comments

so you mean for inserting 10 image, he have to make 10 query to database ??
@@Alireza Fallah... that is his requirement
is it possible to upload images using multiple browse buttons?
1
$images = $_FILES['images']['name'];
$sql = "insert into table (image_name) VALUES  ('".  implode("') ('", $a)."')";

Result is like :

insert into table (image_name) VALUES ('1.jpg') ('2.jpg') ('3.jpg') ('4.jpg')

Comments

0

user foreach.

example code :

foreach ($image as $image_path)
{
    mysql_query("insert into `table` (column) values ('".$image_path."')");
}

4 Comments

you mean for 10 image, he must make 10 queries instead of one??
so u want to 10 images each in one row !! so there will be 10 queries for sure
insert into('pic1.jpg'); insert into('pic2.jpg'); insert into('pic3.jpg'); he want like this i guess
i wanted to loop until all the images are in my database so that will require a long code. so i ask how to use the foreach loop with array instead of for loop its much neater.
0

Try this...

if(isset($_POST['upload'])){
 $total = count($_FILES['images']['name']);
 for($i=0; $i<$total; $i++){

  $tmpFilePath=$_FILES['images']['tmp_name'][$i];
  if($tmpFilePath != ""){


    $img_name = $_FILES['images']['name'][$i];

    mysqli_query($connection, "INSERT INTO table_name (column_name) value ('".$img_name."')");
    }    
  }  
}

Comments

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.