简体   繁体   中英

How to copy the set of files from one folder to another folder using php

I want to copy set of uploaded files from one folder to another folder.From the below code, all the files in one folder is copied.It takes much time. I want to copy only the currently uploaded file to another folder.I have some idea to specify the uploaded files and copy using for loop.But I don't know to implement.I am very new to developing.Please help me.Below is the code.

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


?>

To copy only the uploaded files, there is only a slight change in the coding which I have made. That is instead of using " . " from one folder, I passed the array value. So that only the files which are uploaded will be copied to the new folder instead of copying everything which takes long time.Below is the only change made to do:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM