尝试上传多个文件有不同的输入名称。由于我只需要1个插入查询,我需要将它们上传到数组中。我需要将文件名插入同一行的不同列
if(isset($_FILES['MainImage'])){
$main_image_name = $_FILES['MainImage']['name'];
$main_image_size = $_FILES['MainImage']['size'];
$main_image_tmp = $_FILES['MainImage']['tmp_name'];
$uploadMainTo = $uploadLocation.$main_image_name;
$moveMain = move_uploaded_file($main_image_tmp,$uploadMainTo);
}
if(isset($_FILES['PDF'])){
$pdf_name = $_FILES['PDF']['name'];
$pdf_size = $_FILES['PDF']['size'];
$pdf_tmp = $_FILES['PDF']['tmp_name'];
$uploadPdfTo = $uploadLocation.$pdf_name;
$movepdf = move_uploaded_file($pdf_tmp,$uploadPdfTo);
}
我的表单如下:
<input type="file" name="PDF">
<input type="file" name="MainImage">
查询:
$query = $db->execute("INSERT INTO users (pdf=?, main_image=?) VALUES (?,?) WHERE ID=$user_id", array($pdf, $main_image) );
答案 0 :(得分:2)
正如你在评论中所说:
我需要同时插入2张图像(2个输入字段)和1个pdf文件
因此,我假设您将在HTML标记中包含所有文件输入,如下所示:
<input type="file" name="PDF">
<input type="file" name="MainImage">
<input type="file" name="SecondImage">
在这种情况下,要将上传文件的文件路径插入数据库,这样的事情应该有效:
$uploadMainTo = null;
if(isset($_FILES['MainImage'])){
$main_image_name = $_FILES['MainImage']['name'];
$main_image_size = $_FILES['MainImage']['size'];
$main_image_tmp = $_FILES['MainImage']['tmp_name'];
$uploadMainTo = $uploadLocation.$main_image_name;
$moveMain = move_uploaded_file($main_image_tmp,$uploadMainTo);
}
$uploadSecondTo = null;
if(isset($_FILES['SecondImage'])){
$second_image_name = $_FILES['SecondImage']['name'];
$second_image_size = $_FILES['SecondImage']['size'];
$second_image_tmp = $_FILES['SecondImage']['tmp_name'];
$uploadSecondTo = $uploadLocation.$second_image_name;
$moveSecond = move_uploaded_file($second_image_tmp,$uploadSecondTo);
}
$uploadPdfTo = null;
if(isset($_FILES['PDF'])){
$pdf_name = $_FILES['PDF']['name'];
$pdf_size = $_FILES['PDF']['size'];
$pdf_tmp = $_FILES['PDF']['tmp_name'];
$uploadPdfTo = $uploadLocation.$pdf_name;
$movepdf = move_uploaded_file($pdf_tmp,$uploadPdfTo);
}
$query = $db->execute("INSERT INTO users (pdf, main_image, second_image) VALUES (?,?,?) WHERE ID = ?", array($uploadPdfTo, $uploadMainTo, $uploadSecondTo, $user_id) );
我不确定是否必须使用$uploadXTo
或null
初始化'NULL'
个变量。如果您有问题,请以这种方式进行测试。
答案 1 :(得分:1)
首先纠正你的HTML
<input name="userfile[]" type="file" /><br />
<input name="userfile[]" type="file" /><br />
然后你可以使用php数组
$_FILES['userfile']['name'][0]
$_FILES['userfile']['tmp_name'][0]
$_FILES['userfile']['size'][0]
$_FILES['userfile']['type'][0]
检查php文档:Uploading multiple files