drvc63490
2015-08-31 14:21
浏览 340
已采纳

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

I want to upload and store multiple files/pics path in database with 1 submit button ...... This is what i am doing in html form

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

And this is my php code

<?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'  )";

}

But its not working ...... Any advice will be appreciated .. Thanks in advance

图片转代码服务由CSDN问答提供 功能建议

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

 图标:
&lt; br&gt; 
&lt; br&gt; 
&lt; input type =“file”name =“uploadedfile”&gt  ; 
&lt; br&gt; 
&lt; br&gt; 
Screenshot:
&lt; br&gt; 
&lt; br&gt; 
&lt; input type =“file”name =“fileToUpload”&gt;  
&lt; br&gt; 
   
 
 

这是我的PHP代码

 &lt;?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“上传成功”; 
 
} 
else {
 echo“ 未成功上传“; 
} 
 
 $ targets_dir =”images /“; 
 $ targets_file = $ targets_dir.basename($ _ FILES ['fileToUpload'] ['name']); 
 $ tmps = $  _FILES ['fileToUpload'] ['tmp_name']; 
 
if(move_uploaded_file($ tmps,$ targets_file)){
 
 echo“上传成功”; 
 
} 
else {
 echo“not not 上传成功“; 
} 
 
 $ insert =”插入应用值(DEFAULT,'$ Title','$ target_file','$ targets_file')“; 
 
} 
    
 
 

但它不起作用...... 任何建议都将受到赞赏.. 谢谢提前

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongyi7513 2015-08-31 14:40
    已采纳

    Use this code, u just need to insert the query. Rest everything will work fine.

     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
        }
    }
    
    已采纳该答案
    打赏 评论
  • duanqiechui2378 2015-08-31 14:30

    You can do multiple file uploads from a single input element.

    For your case, you can just rename both of your input element's name to be something like file[]. The array ([]) is important and tells that file is really an array of multiple files.

    On the server side, the first file name (your icon) can be accessed like $_FILES['file']['name'][0] and the second name (the screenshot) like $_FILES['userfile']['name'][1].

    You can review more options in the documentation.

    打赏 评论

相关推荐 更多相似问题