我上传了多个文件,但仅循环播放一次并停止 这意味着我只能在数据库中获得一个文件
我尝试将db部分插入循环中,但它在db中的多行 而且我不想要这样
if(isset($_POST['submit'])){
$websites = $_POST['websites'];
foreach($_FILES['documents']['tmp_name'] as $key => $tmp_name){
$file_name = $key.$_FILES['documents']['name'][$key];
$file_size =$_FILES['documents']['size'][$key];
$file_tmp =$_FILES['documents']['tmp_name'][$key];
$file_type=$_FILES['documents']['type'][$key];
move_uploaded_file($file_tmp,"uploads/".time().$file_name);
}
print_r($file_name);
$pdo = DB();
$stmt = $pdo->prepare("INSERT INTO client_form_7
(client_id, documents, websites)
VALUES (:client_id, :documents, :websites)");
$stmt->bindParam("client_id", $user_id, PDO::PARAM_INT);
$stmt->bindParam("documents", $file_name, PDO::PARAM_STR);
$stmt->bindParam("websites", $websites, PDO::PARAM_STR);
$stmt->execute();
}
我希望我所有的文件都以数组形式存储在db中
编辑
我的解决方案
只需在foreach之前创建一个空数组,然后在foreach内部将文件名推送到新数组,并在外部用json_encode
编码完整的文件名并将其插入db,谢谢大家的辛勤工作
代码
$arr = array();
foreach($_FILES['documents']['tmp_name'] as $key => $tmp_name){
$file_name = $key.$_FILES['documents']['name'][$key];
$file_size =$_FILES['documents']['size'][$key];
$file_tmp =$_FILES['documents']['tmp_name'][$key];
$file_type=$_FILES['documents']['type'][$key];
move_uploaded_file($file_tmp,"uploads/".time().$file_name);
$arr[] = $file_name;
}
$files = json_encode($arr);