drmgg4411 2013-02-06 10:32
浏览 73
已采纳

使用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>
  • 写回答

1条回答 默认 最新

  • doutang6600 2013-02-06 11:04
    关注
    <?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()).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备