Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9257035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:06:37+00:00 2026-06-18T12:06:37+00:00

I am intending to upload many images at once into a folder and save

  • 0

I am intending to upload many images at once into a folder and save their information into a db.

As it stands the code does upload these images into the folder correctly regardless of if I select one or many. However when I select many it does not store the information of the images into the database but only uploads the images into the folder. If I select one image then it stores that image’s info into the db correctly.

Below is my code .

<?php
#connect to the db
require_once('db.inc.php');
?>
<html>
<head>
</head>
<body>
<form action="" method="POST" enctype="multipart/form-data">
    <input type="file" name="files[]" multiple/>
    <input type="submit"/>
</form>
<?php
if(isset($_FILES['files'])){
    $errors= array();
    foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
        $file_name = $key.$_FILES['files']['name'][$key];
        $file_size =$_FILES['files']['size'][$key];
        $file_tmp =$_FILES['files']['tmp_name'][$key];
        $file_type=$_FILES['files']['type'][$key];  
        if($file_size > 2097152){
            $errors[]='File size must be less than 2 MB';
        }try{       
$query ="INSERT into tish_images(FILE_NAME,FILE_SIZE,FILE_TYPE)
VALUES(:FILE_NAME,:FILE_SIZE,:FILE_TYPE)";
$insert = $con->prepare($query);
$insert->execute(array(
':FILE_NAME'=>$file_name,
':FILE_SIZE'=>$file_size,
':FILE_TYPE'=>$file_type));
}catch(PDOException $e){
echo $e->getMessage();
}
$desired_dir="image_uploads";
        if(empty($errors)==true){
            if(is_dir($desired_dir)==false){
                mkdir("$desired_dir", 0700);// Create directory if it does not exist
            }
            if(is_dir("$desired_dir/".$file_name)==false){
                move_uploaded_file($file_tmp,"image_uploads/".$file_name);
            }else{                                  //rename the file if another one exist
                $new_dir="image_uploads/".$file_name.time();
                 rename($file_tmp,$new_dir) ;               
            }


        }else{
                print_r($errors);
        }
    }
    if(empty($error)){
        echo "Success";
    }
}
?>

</body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T12:06:38+00:00Added an answer on June 18, 2026 at 12:06 pm
    <?php
    if(isset($_FILES['files'])){
        $query = "INSERT into tish_images(`FILE_NAME`,`FILE_SIZE`,`FILE_TYPE`)
                 VALUES(:FILE_NAME,:FILE_SIZE,:FILE_TYPE)";
        $stmt  = $con->prepare($query);
        $errors= array();
        foreach($_FILES['files']['tmp_name'] as $key => $error ){
            if ($error != UPLOAD_ERR_OK) {
                $errors[] = $_FILES['files']['name'][$key] . ' was not uploaded.';
                continue;
            }
            $file_name = $key.$_FILES['files']['name'][$key];
            $file_size = $_FILES['files']['size'][$key];
            $file_tmp  = $_FILES['files']['tmp_name'][$key];
            $file_type = $_FILES['files']['type'][$key];  
            if($file_size > 2097152){
                $errors[] = 'File size must be less than 2 MB';
                continue;
            }
            try{       
                $stmt->bindParam( ':FILE_NAME', $file_name , PDO::PARAM_STR );
                $stmt->bindParam( ':FILE_SIZE', $file_size, PDO::PARAM_STR );
                $stmt->bindParam( ':FILE_TYPE', $file_type, PDO::PARAM_STR );
                $stmt->execute();
    
                $desired_dir="image_uploads";
    
                if(is_dir($desired_dir)==false){
                    mkdir($desired_dir, 0700);// Create directory if it does not exist
                }
                if(is_file($desired_dir.'/'.$file_name)==false){
                    move_uploaded_file($file_tmp,$desired_dir.'/'.$file_name);
                }else{    //rename the file if another one exist
                    $new_file=$desired_dir.'/'.$file_name.time();
                    move_uploaded_file($file_tmp,$new_file) ;               
                }
            }catch(PDOException $e){
                $errors[] = $file_name . 'not saved in db.';
                echo $e->getMessage();
            }   
        }
        if(empty($error)){
            echo "Success";
        }
    }
    ?>
    

    I can’t see too much wrong with that but I’ve refactored the code around pdo a little so its more readable and you only prepare once now. I’ve moved the actual move file inside the try block too so you only attempt when no error in db execution (you should check the status of the insert – look at $stmt->errorCode() or $stmt->errorInfo()).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm intending to use JCrop for a standard image upload feature. Everything is all
I'm intending to create a flash app that pulls images from facebook and displays
I'm intending to use the new .NET 4 Code Contracts feature for future development.
I am intending to use the n-gram code from this article . The algorithm
My code is simply intending to populate two drop down lists with the names
I am intending to use SQLite 3 with PHP 5. I found this: http://packages.debian.org/etch/web/php5-sqlite3
I am intending to create a small api, that will do some php functions,
I am still developing this function, but here is what I am intending it
I'm starting to do a small amount of development within my company. I'm intending
So what I'm intending to do here is to determine both the latest major

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.