Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 820863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:32:47+00:00 2026-05-15T02:32:47+00:00

I am using the following code in WINDOWS OS and PHP script, in which

  • 0

I am using the following code in WINDOWS OS and PHP script, in which initially i am taking the difference of two folder structure and then the out put needs to be copied to other folder. here is the code below..

$source = '/var/www/html/copy1';
$mirror = '/var/www/html/copy2';
function scan_dir_recursive($dir, $rel = null) {

  $all_paths = array();
  $new_paths = scandir($dir);

  foreach ($new_paths as $path) {

    if ($path == '.' || $path == '..') {
      continue;
    }

    if ($rel === null) {
        $path_with_rel = $path;
    } else {
        $path_with_rel = $rel . DIRECTORY_SEPARATOR . $path;
    }

    $full_path = $dir . DIRECTORY_SEPARATOR . $path;
    $all_paths[] = $path_with_rel;

    if (is_dir($full_path)) {
      $all_paths = array_merge(
        $all_paths, scan_dir_recursive($full_path, $path_with_rel)
      );
    }

  }

  return $all_paths;

}
$diff_paths = array_diff(
    scan_dir_recursive($mirror),
    scan_dir_recursive($source)
);


/*$diff_path = array_diff($mirror,$original);*/
echo "<pre>Difference ";print_r($diff_paths);
    Difference of Folders Array
(
    [4] => New Folder (2)
    [5] => New Folder (2)/New Folder
    [6] => New Folder (2)/New Folder/New Folder
    [7] => New Folder (2)/New Folder/New Folder/New Text Document (2).txt
    [8] => New Folder (2)/New Folder/New Folder/New Text Document.txt
)

foreach($diff_paths as $path)
{
    echo $source1 = "var/www/html/copy2/".$path;
    echo "<br>";
    $des = "var/www/html/copy1/".$path;
    copy_recursive_dirs($source1, $des);
}

function copy_recursive_dirs($dirsource, $dirdest)
{ 
   $dir_handle=opendir($dirsource);

    mkdir($dirdest,0777);

 while(false!==($file=readdir($dir_handle)))
 {/*echo "<pre>";
  print_r($file);*/
     if($file!="." && $file!="..")
     {
         if(is_dir($dirsource.DIRECTORY_SEPARATOR.$file)) 
         {
            //Copy the file at the same level in the destination folder
            copy_recursive_dirs($dirsource.DIRECTORY_SEPARATOR.$file, $dirdest.DIRECTORY_SEPARATOR.$file);

         }
         else{
            //Copy the dir at the same lavel in the destination folder
             copy ($dirsource.DIRECTORY_SEPARATOR.$file, $dirdest.DIRECTORY_SEPARATOR.$file);

         }
     }
  }
 closedir($dir_handle);
 return true;
}

Whenever I execute the script I get the difference output but do not get the other copy on second folder as per code… Pls help me in rectifying…

UPDATE
I just want to copy the difference to
other folder, if theere is any other
way pls help me….

UPDATE:
I am getting these errors,

( ! ) Warning:
opendir(var/www/html/copy2/New Folder
(2)) [function.opendir]: failed to
open dir: No such file or directory in
/var/www/html/pranav_test/syncss.php
on line 96

( ! ) Warning: mkdir()
[function.mkdir]: No such file or
directory in
/var/www/html/pranav_test/syncss.php
on line 99

( ! ) Warning: readdir(): supplied
argument is not a valid Directory
resource in
/var/www/html/pranav_test/syncss.php
on line 104

( ! ) Warning: closedir(): supplied
argument is not a valid Directory
resource in
/var/www/html/pranav_test/syncss.php
on line 122

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T02:32:48+00:00Added an answer on May 15, 2026 at 2:32 am

    Do not try to do everything at once.
    Separate your task into smaller chunks.
    And pass to the next one only if you had finishad and tested previous part.

    First, learn to find the difference between 2 arrays:
    Hardcode 2 slightly different arrays in your script and play with array_diff() function

    Next, learn to read files from directory. Not recursive, just one.
    End up with array of filenames.
    Test it with print_r()

    Now you can try to read 2 directories into arrays and compare it.
    If it still not working, debug it: print out content of arrays, compare it with your eyes. Ask for help on SO for this particular and certain problem with straight and clear input data.
    end up with an array of differences, printed on the screen to be sure it contains actual data.

    Well, now you can go for the copy part. Same technique.

    And then you can go for recursive directories. Same technique – double-checking and testing on every stage. Print out as much debugging information as possible.

    Turn on error reporting and ensure you can see every error occurred (just make an intentional one and see). every filesystem operation will throw an error on failure. So, you will see the reason. And debug filesystem operations as well. Print out from inside the conditions to see if it ever got executed. Print out variable contents, function return values.

    Ever got curious, what is this $dirsource.DIRECTORY_SEPARATOR.$file line’s actual value?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 407k
  • Answers 407k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer For direct access, you would have to use SSL on… May 15, 2026 at 6:23 am
  • Editorial Team
    Editorial Team added an answer I finally found the answer on MySQL website. In case… May 15, 2026 at 6:23 am
  • Editorial Team
    Editorial Team added an answer open (MYFILE, '>>data.txt') — Open data.txt, keep the original data,… May 15, 2026 at 6:23 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.