如何使用php和mysqli在一个事务中上载多个图像文件?
我试图在每个事务的数据库中插入多个图像,但每个图像都有自己的输入类型。但是当我提交表单时,我得到的错误是我的如何使用php和mysqli在一个事务中上载多个图像文件?,php,html,mysqli,input-type-file,Php,Html,Mysqli,Input Type File,我试图在每个事务的数据库中插入多个图像,但每个图像都有自己的输入类型。但是当我提交表单时,我得到的错误是我的$file\u upload=$\u FILES['file\u upload.$I]是一个“未定义的索引”。我的HTML中的文件上传1,文件上传2,文件上传3是否不是正确的方法?请帮忙。多谢各位 我的php代码是: include 'dbcontroller.php'; if(isset($_POST['submit'])) { for ($i = 1; $i <= 3;
$file\u upload=$\u FILES['file\u upload.$I]是一个“未定义的索引”。我的HTML中的文件上传1
,文件上传2
,文件上传3
是否不是正确的方法?请帮忙。多谢各位
我的php代码是:
include 'dbcontroller.php';
if(isset($_POST['submit'])) {
for ($i = 1; $i <= 3; $i++) {
$file_upload = $_FILES['file_upload'.$i];
$file=(rand(1000,100000)."-".$file_upload['name'.$i]);
$type=$file_upload['type'.$i];
$size=$file_upload['size'.$i];
$loc=$file_upload['tmp_name'.$i];
$new_size=$size/1024; // file size in KB
// make file name in lower case
$new_file_name = strtolower($file);
// make file name in lower case
$final_file=str_replace(' ','-',$new_file_name);
if(move_uploaded_file($loc, '..admin/officers-avatars/'.$final_file)) {
$result = mysqli_query($conn,"INSERT INTO images VALUES ('$final_file', '$new_size', '$type')")
or die(mysqli_error($conn));
}
}
}
包括“dbcontroller.php”;
如果(isset($_POST['submit'])){
对于($i=1;$i您有一个输入错误:enctype=“多份/表格数据”
对于enctype=“多份/表格数据”
无论如何,我建议您使用name
属性中的数组:
<form action="post.php" method="post" enctype="multipart/form-data">
<input type="file" name="fileupload[]" /><br><br>
<input type="file" name="fileupload[]" /><br><br>
<input type="file" name="fileupload[]" /><br><br>
<input type="submit" name="submit" value="SAVE"/>
</form>
或者,您可以使用多个:
<input type="file" name="fileupload[]" multiple /><br><br>
你做过var\u dump()吗$\u FILES
上的以查看实际存在的内容?不,我没有。以前也没有尝试过var\u dump
,但我会尝试一下。@eatpeanutbutter成功了。我得到了一个NULL.Wew@eatpeanutbutter,这意味着您的文件没有正确上载到服务器。如果@filippe的建议不起作用,请参阅此清单这可能会出错:谢谢。这个链接实际上帮助了我。@EatPeanutButterI建议使用
,但请注意允许用户直接上传到数据库中的内容,你可能会将一些有害病毒上传到你的网站上,而不知道它们是如何或从何而来的。此外,请检查enctype部分。你需要使用e> 多部分
而不是多部分
我尝试了您的代码,但出现了一个错误。注意:未定义索引:fileupload
和警告:为foreach()提供的参数无效。
都在同一行。感谢您注意我的打字错误:)我发现了我的错误,只是我的HTML文件名和我的php文件名不一样,这就是为什么我得到了一个未定义的错误。修复了它,现在开始工作。你的回答很有用,谢谢。
<input type="file" name="fileupload[]" multiple /><br><br>