【问题标题】:Upload to multiple directories上传到多个目录
【发布时间】:2014-11-25 10:34:07
【问题描述】:

在我的应用程序中,我想将同一个文件保存在两个不同的目录中。在我的 www 文件夹中,我有两个名为 folder1 和 folder2 的文件夹。我将图像上传到文件夹 1 中的“上传”目录。我想将此图像移动到位于文件夹 2 内的名为“上传”的文件夹中。

这是我的代码。

    $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."; 
 } 
 ?>

我使用了“复制()”。作为一个新的网络开发人员,我不知道它是否正确。我的upload.php 位于folder1 中。谁能帮帮我。

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    使用文件复制功能

    copy()

    您是第一次上传文件。

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

    你需要从你的第一个目录复制资源。

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

    【讨论】:

      【解决方案2】:

      试试 -

      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']);
      

      【讨论】:

      • 我试过了。但这是行不通的。我不知道给 target2 正确与否的语法。
      • 检查文件结构。两个文件夹的路径级别应该相同。
      【解决方案3】:

      您在这段代码中几乎 90% 是正确的。您只需要添加一行(即代码下方的第四行)

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

      【讨论】:

        猜你喜欢
        • 2020-02-21
        • 1970-01-01
        • 1970-01-01
        • 2021-05-25
        • 2023-03-05
        • 1970-01-01
        • 2021-04-04
        • 2021-11-05
        • 2011-05-30
        相关资源
        最近更新 更多