如何使用php将文件集从一个文件夹复制到另一个文件夹

时间:2015-04-30 13:39:36

标签: php

我想将一组上传的文件从一个文件夹复制到另一个文件夹。从下面的代码中,复制了一个文件夹中的所有文件。这需要很长时间。我想只将当前上传的文件复制到另一个文件夹。我有一些想法指定上传的文件并使用for循环复制。但我不知道要实现。我是一个非常新的开发。请帮助我.Below是代码。

<?php 

 // connect to the database
 include('connect-db.php');

 if (isset($_POST['submit']))
 { 
 // get form data, making sure it is valid
 $udate = mysql_real_escape_string(htmlspecialchars($_POST['udate']));
 $file_array=($_FILES['file_array']['name']);



 // check to make sure both fields are entered
 if ($udate == '' || $file_array=='')
 {
 // generate error message
 $error = 'ERROR: Please fill in all required fields!';

 // if either field is blank, display the form again
 renderForm($udate, $file_array, $error);
 }
 else
 {
     $udate = mysql_real_escape_string(htmlspecialchars($_POST['udate']));
if(isset($_FILES['file_array']))
{
    $name_arrray=$_FILES['file_array']['name'];
    $tmp_name_arrray=$_FILES['file_array']['tmp_name'];
    for($i=0;$i <count($tmp_name_arrray); $i++)
    {
        if(move_uploaded_file($tmp_name_arrray[$i],"test_uploads/".str_replace(' ','',$name_arrray[$i])))

        {

                       // save the data to the database
$j=str_replace(' ','',$name_arrray[$i]);
echo $j;
 $udate = mysql_real_escape_string(htmlspecialchars($_POST['udate']));
  $provider = mysql_real_escape_string(htmlspecialchars($_POST['provider']));
  $existfile=mysql_query("select ubatch_file from batches");
  while($existing = mysql_fetch_array( $existfile)) {
      if($j==$existing['ubatch_file'])
    echo'  <script>
function myFunction() {
    alert("file already exists");
}
</script>';

      }

 mysql_query("INSERT IGNORE batches SET udate='$udate', ubatch_file='$j',provider='$provider',privilege='$_SESSION[PRIVILEGE]'")
 or die(mysql_error()); 
        echo $name_arrray[$i]."uploaded completed"."<br>";
        $src = 'test_uploads';
$dst = 'copy_test_uploads';
$files = glob("test_uploads/*.*");
      foreach($files as $file){
      $file_to_go = str_replace($src,$dst,$file);
      copy($file, $file_to_go);

       /* echo "<script type=\"text/javascript\">
                        alert(\"CSV File has been successfully Uploaded.\");
                        window.location = \"uploadbatches1.php\"
                    </script>";*/
      }
        } else
        {
            echo "move_uploaded_file function failed for".$name_array[$i]."<br>";
        }

    }
}

 // once saved, redirect back to the view page
 header("Location:uploadbatches1.php"); 
 }
 }
 else
 // if the form hasn't been submitted, display the form
 {
 renderForm('','','');
 }


?>

1 个答案:

答案 0 :(得分:0)

要仅复制上传的文件,我所做的编码只有轻微的变化。这不是使用&#34; &#34;从一个文件夹,我传递了数组值。因此,只有上传的文件才会被复制到新文件夹而不是复制需要很长时间的所有文件。下面是唯一要做的更改:

$files = glob("test_uploads/$name_arrray[$i]");