使用pdo在php中上传多个图像

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>
<?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()).