上传到多个目录

时间:2014-11-25 10:34:07

标签: php mysql

在我的应用程序中,我想将相同的文件保存在两个不同的目录中。在我的www文件夹中,我有两个文件夹调用folder1和folder2。我将图像上传到folder1中的'uploads'目录。我想将此图像移动到位于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.can中,任何人都可以帮助我。

3 个答案:

答案 0 :(得分: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']);

答案 1 :(得分:0)

使用文件复制功能

copy()

您是第一次上传文件。

因此,文件资源正从其临时位置移动。

您需要从第一个目录中复制资源。

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

答案 2 :(得分:0)

您有近90%的权利在此代码中。你只需要添加一行(即从下面第四行到你的代码)

$target = "uploads/"; 

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