2014-11-25 64 views
0

在我的應用程序中,我想將相同的文件保存在兩個不同的目錄中。在我的www文件夾中,我有兩個文件夾調用folder1和folder2。我將圖像上傳到folder1中的「上傳」目錄。我想將此圖像移至位於folder2內的名爲'uploads'的文件夾。上傳到多個目錄

這是我的代碼。

$target = "uploads/"; 

$target = $target . basename($_FILES['photo']['name']); 
$target2="folder2/uploads/"; 

//This gets all the other information from the form 

$desc=$_POST['desc']; 
$pic=($_FILES['photo']['name']); 
$loc=$_POST['location']; 


// Connects to your Database 
mysql_connect("localhost", "root", "") or die(mysql_error()) ; 
mysql_select_db("selfie") or die(mysql_error()) ; 



$filename = mysql_real_escape_string($_FILES['photo']['name']); 
//Writes the information to the database 
mysql_query("INSERT INTO image_upload (description,image,location) VALUES  ('$desc','$pic','$loc')"); 


//Writes the photo to the server 

if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
{ 
copy($target, $target2); 
    } 
else { 

//Gives and error if its not 
echo "Sorry, there was a problem uploading your file."; 
} 
?> 

我用'copy()'。作爲一名新的Web開發人員,我不知道它是否正確。我的upload.php位於folder1中,任何人都可以幫助我。

回答

0

嘗試用 -

if(move_uploaded_file($_FILES['photo']['tmp_name'], $target) && move_uploaded_file($_FILES['photo']['tmp_name'], $target2)) { //you code 

copy($target, $target2.basename($_FILES['photo']['name']); 
+0

我試過了。但這不起作用。我不知道賦予target2的語法是否正確。 – Lanka 2014-11-25 10:44:25

+0

檢查文件結構。兩個文件夾的路徑級別應該相同。 – 2014-11-25 10:48:09

0

使用文件拷貝功能

copy()

你上傳的首次文件。

因此,文件資源正在從其臨時位置移動。

您需要從第一個目錄複製資源。

$target2 = "folder2/uploads/". basename($_FILES['photo']['name']); 
copy($target, $target2); 
+0

試過。但不工作:( – Lanka 2014-11-25 11:00:22

0

您在此代碼中擁有將近90%的權利。您只需添加一行(即從第四行到您的代碼)

$target = "uploads/"; 

$target = $target . basename($_FILES['photo']['name']); 
$target2="folder2/uploads/"; 
$target2 = $target2 . basename($_FILES['photo']['name']);