2017-09-25 182 views
-1

我想从一个输入上传两个文件,两个文件都出现在uploads文件夹中,因为它们应该这样做,但只有一个文件路径进入数据库。上传文件路径到数据库

E.g.如果我上传test1.pdf和test2.pdf,它们都将进入上传文件夹,但test1.pdf将被插入数据库的两列而不是一列。

在我疯了之前,有谁能帮助我吗?

if (count($_FILES['field2']['name']) >= 1) { 


    //Loop through each file 
    for ($i = 0; $i < count($_FILES['field2']['name']); $i++) { 
     //Get the temp file path 
     $tmpFilePath = $_FILES['field2']['tmp_name'][$i]; 

     //Make sure we have a filepath 
     if ($tmpFilePath != "") { 

      //save the filename 
      $shortname = $_FILES['field2']['name'][$i]; 

      //save the url and the file 
      $filePath = "uploads/" . date('d-m-Y-H-i-s') . '-' . $_FILES['field2']['name'][$i]; 
      $filePath1 = "uploads/" . date('d-m-Y-H-i-s') . '-' . $_FILES['field2']['name'][$i]; 

      //Upload the file into the temp dir 
      if (move_uploaded_file($tmpFilePath, $filePath)) { 


    $sql = "INSERT INTO " . $config_tbl_prefix . " subcontractor_qs   (field1, field2) 

    VALUES 
    ('$filePath','$filePath1')"; 

    mysql_query($sql); 

     } 
     } 
    } 
    } 
} 
+0

为什么你使用的是mysql *函数,它在PHP的新版本中被弃用,并在PHP7中被删除。 –

+0

你需要一个循环。 – Script47

+0

@RossH如果你不在乎,为什么你还在发展? – Script47

回答

0
if (count($_FILES) >= 1) { 


//Loop through each file 
foreach ($_FILES as $id=>$file) { 
    //Get the temp file path 
    $tmpFilePath = $file['tmp_name']; 

    //Make sure we have a filepath 
    if ($tmpFilePath != "") { 

     //save the filename 
     $shortname = $file['name']; 

     //save the url and the file 
     $filePath = "uploads/" . date('d-m-Y-H-i-s') . '-' . $file['name']; 

     //Upload the file into the temp dir 
     if (move_uploaded_file($tmpFilePath, $filePath)) { 


     $sql = "INSERT INTO " . $config_tbl_prefix . " subcontractor_qs (field1) VALUES ('$filePath')"; 

     mysql_query($sql); 

     } 
     } 
    } 
    } 
} 
+0

是不是只能上传一个文件,这是我现在遇到的同样的问题。我需要一次上传多个。 – Ross

0
,如果你想使用申请,然后更新您的代码相同的输入上传二个或多个文件

。在此代码中保存文件名和路径。一次你不能使用循环获取两个文件路径。

1.重要的事情删除MySQL和使用PDO或mysqli的

<if (count($_FILES['enviroupload']['name']) >= 1) { 
//Loop through each file 
for ($i = 0; $i < count($_FILES['enviroupload']['name']); $i++) { 
    //Get the temp file path 
    $tmpFilePath = $_FILES['enviroupload']['tmp_name'][$i]; 

    //Make sure we have a filepath 
    if ($tmpFilePath != "") { 

     //save the filename 
     $shortname = $_FILES['enviroupload']['name'][$i]; 

     //save the url and the file 
     $filePath = "uploads/" . date('d-m-Y-H-i-s') . '-' . $_FILES['enviroupload']['name'][$i]; 

     //Upload the file into the temp dir 
     if (move_uploaded_file($tmpFilePath, $filePath)) { 

      $sql = "INSERT INTO " . $config_tbl_prefix . " subcontractor_qs   (field1, field2) 

VALUES 
('$shortname','$filePath')"; 

      //mysql_query($sql); 
      echo $sql; 
      echo "<br>"; 

     } 
    } 
} 

}

在此代码文件的路径和文件名

将保存数据库,如果你只想保存文件路径那么需要更新你的代码

+0

评论不适用于扩展讨论或调试会话,我们不鼓励用户在本网站上进行互动。应该将其他信息编辑到问题或答案中。 –

相关问题