1

Could someone please assist as I would like to upload multiple images to the server as well the image names to the database.

The below code works for uploading the images to the server:

View:

      <form action="<?php echo URLROOT; ?>/posts/add" method="post" enctype='multipart/form-data'>
        <input type="file" name="file[]" multiple>
        <input type="submit" name=submit value="Submit">
      </form>

Controller:

  
        if($_SERVER['REQUEST_METHOD'] == 'POST'){
 
          // Count total files
          $countfiles = count($_FILES['file']['name']);
          
          // Looping all files
          for($i=0;$i<$countfiles;$i++){
            $filename = $_FILES['file']['name'][$i];
            
            // Upload file
            move_uploaded_file($_FILES['file']['tmp_name'][$i], dirname(__DIR__)."/img/".$filename);
             
          }
              } else {
          // Load view with errors
          $this->view('posts/add');
        }

      }

I am also able to upload the 3 image names when using the below code with 3 separate inputs:

View:

    <form action="<?php echo URLROOT; ?>/posts/add" method="post">
        <input type="file" name="image1">
        <input type="file" name="image2">
        <input type="file" name="image3">
        <input type="submit" name=submit value="Submit">
      </form>

Controller:

      if($_SERVER['REQUEST_METHOD'] == 'POST'){
        // Sanitize POST array
        $_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);

        $data = [
          'user_id' => $_SESSION['user_id'],

          'image1' => trim($_POST['image1']),
          'image2' => trim($_POST['image2']),
          'image3' => trim($_POST['image3']),
        ];

    
        // Validated
          if($this->postModel->addPost($data)){
            flash('post_message', 'Post Added');
            redirect('posts');
          } else {
            die('Something went wrong');
          }      

      } else {
  
        $data = [
           'image1' => '',
           'image2' => '',
           'image3' => ''
          ];
  
        $this->view('posts/add', $data);
      }
    }

Could someone please advise on how I could combine the code, in order to use a single file input for uploading the images to server, as well as the image names to the database?

5
  • you want only one input type file for upload multiples images in codeigniter? Commented Feb 27, 2021 at 11:28
  • @KUMAR thank you for the reply. Yes, I would like to use only one input. for multiple images. It is for a custom MVC framework. Commented Feb 27, 2021 at 11:33
  • okay please wait. Commented Feb 27, 2021 at 11:34
  • now see my updated answer please. Commented Feb 27, 2021 at 11:57
  • There are many duplicates here on SO, did you try any of those? Commented Feb 27, 2021 at 12:00

2 Answers 2

1

Complete Code

<form method='post' action='' enctype='multipart/form-data'>
  <input type="file" name="file[]" id="file" multiple>
 <input type='submit' name='submit'  value='Upload'>
</form>

PHP

<?php 
if(isset($_POST['submit'])){
       
      $imageName =$_FILES['file']['name'];
 
     // Count total files
 $countfiles = count($_FILES['file']['name']);
 
 // Looping all files
 for($i=0;$i<$countfiles;$i++){
   $filename = $_FILES['file']['name'][$i];
   
   // Upload file
   move_uploaded_file($_FILES['file']['tmp_name'][$i],'upload/'.$filename);

    //insert code 

 $query = "insert into images(images) values('".$imageName."')";
 mysqli_query($con,$query);
 }
} 
?>
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the code. This code works for uploading the images to server. Could you perhaps please advise on how I could add the part for uploading the image names to the database?
by using insert query
Thank you. Could you please advise how I would be able to declare or separate the different image or file names that are currently stored in the $filename variable?
0

Use this input code -

<input type="file" name="image[]" id="file" multiple>

And count file in php file then use loop for iterating file like-

$counts = count($_FILES['image']['name']);
for($i=0;$i<$counts;$i++){
//uploading code here
}

1 Comment

Thank you for your reply. This works for the image upload part, but I am not sure how to get the different image names in order to declare them as variables for the purpose of uploading the different image names in different columns, in the database.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.