我打算一次将许多图像上传到文件夹中,并将其信息保存到数据库中。
不管我是选择一个还是多个,代码都会正确地将这些图像上传到文件夹中。但是,当我选择多个时,它不会将图像的信息存储到数据库中,而只会将图像上传到文件夹中。如果我选择一个图像,那么它会正确地将该图像的信息存储到数据库中。
以下是我的代码。
<?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>
答案 0 :(得分:7)
<?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";
}
}
?>
我无法看到太多错误,但我已经对pdo周围的代码进行了重构,因此它更具可读性,现在只准备一次。我已经在try块中移动了实际的移动文件,所以你只在db执行中没有错误时尝试(你应该检查插入的状态 - 看看$ stmt-&gt; errorCode()或$ stmt-&gt ; errorInfo中())