2017-09-25 181 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

評論不適用於擴展討論或調試會話,我們不鼓勵用戶在本網站上進行互動。應該將其他信息編輯到問題或答案中。 –

相關問題