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

如何使用php将文件集从一个文件夹复制到另一个文件夹,php,Php,我想将一组上载的文件从一个文件夹复制到另一个文件夹。根据下面的代码,将复制一个文件夹中的所有文件。这需要很多时间。我只想将当前上传的文件复制到另一个文件夹。我有一些想法来指定上传的文件并使用for循环进行复制。但我不知道如何实现。我对开发非常陌生。请帮助我。下面是代码 <?php // connect to the database include('connect-db.php'); if (isset($_POST['submit'])) { // get form

我想将一组上载的文件从一个文件夹复制到另一个文件夹。根据下面的代码,将复制一个文件夹中的所有文件。这需要很多时间。我只想将当前上传的文件复制到另一个文件夹。我有一些想法来指定上传的文件并使用for循环进行复制。但我不知道如何实现。我对开发非常陌生。请帮助我。下面是代码

<?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('','','');
 }


?>

为了只复制上传的文件,我只对编码做了一点小小的修改。这不是从一个文件夹中使用“.”,而是传递数组值。这样,只有上载的文件才会复制到新文件夹,而不是复制所有需要很长时间的内容。下面是唯一要做的更改:

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