使用1个提交按钮上传多个文件/图片

时间:2015-08-31 14:21:55

标签: php html

我想用1个提交按钮上传和存储数据库中的多个文件/图片路径...... 这就是我用html格式做的事情

Icon:
<br>
<br>
<input type="file"     name="uploadedfile" >
<br>
<br>
Screenshot:
<br>
<br>
<input type="file"  name ="fileToUpload"> 
<br>

这是我的PHP代码

<?php 

if (isset($_POST['submit'])){
$target_dir= "images/";
$target_file= $target_dir.basename($_FILES['uploadedfile']['name']);
$tmp=$_FILES['uploadedfile']['tmp_name'];
if (move_uploaded_file($tmp,$target_file) ){

    echo "uploaded successfully";

}
else {
    echo "not uploaded successfully";
}

$targets_dir= "images/";
$targets_file= $targets_dir.basename($_FILES['fileToUpload']['name']);
$tmps=$_FILES['fileToUpload']['tmp_name'];

if (move_uploaded_file($tmps,$targets_file) ){

    echo "uploaded successfully";

}
else {
    echo "not uploaded successfully";
}

$insert=" insert into app values  (DEFAULT,'$Title', '$target_file' , '$targets_file'  )";

}

但它不起作用...... 任何建议将被认真考虑 .. 提前致谢

2 个答案:

答案 0 :(得分:0)

您可以从单个input元素中执行multiple file uploads

对于您的情况,您可以将input元素的name重命名为file[]。数组([])很重要,它告诉我file实际上是一个包含多个文件的数组。

在服务器端,可以访问第一个文件名(您的图标),如$_FILES['file']['name'][0],第二个名称(屏幕截图),如$_FILES['userfile']['name'][1]

您可以在documentation中查看更多选项。

答案 1 :(得分:0)

使用此代码,您只需插入查询即可。休息一切都会正常。

 if ((!empty($_FILES['uploadedfile']["name"])) && (!empty($_FILES['fileToUpload']["name"])))
{

$file_name1=$_FILES['uploadedfile']["name"];
$temp_name1=$_FILES['uploadedfile']["tmp_name"];
$imagename1=date("d-m-Y")."-".time();       
$target_path1 = "images/".$imagename1;
$Rtarget_path1 = "images/".$imagename1;

$file_name2=$_FILES['fileToUpload']["name"];
$temp_name2=$_FILES['fileToUpload']["tmp_name"];
$imagename2=date("d-m-Y")."-".time();       
$target_path2 = "images/".$imagename2;
$Rtarget_path2 = "images/".$imagename2;

   if((move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $Rtarget_path1 )) && (move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $Rtarget_path2 )))
    {
        $insert=" insert into app values  (DEFAULT,'$Title', '$target_path1' , '$target_path2'  )"; // Insert Query Wrong. Also Check Here
    }
}