Note that there are some explanatory texts on larger screens.

plurals
  1. PODelete the unwanted files and folders from destination folder as compared to source folder
    primarykey
    data
    text
    <p>I am using PHP and I need to script something like below:</p> <blockquote> <p>I have to compare two folder structure and with reference of source folder I want to delete all the files/folders present in other destination folder which do not exist in reference source folder, how could i do this?</p> <p><strong>EDITED:</strong></p> </blockquote> <pre><code>$original = scan_dir_recursive('/var/www/html/copy2'); $mirror = scan_dir_recursive('/var/www/html/copy1'); function scan_dir_recursive($dir) { $all_paths = array(); $new_paths = scandir($dir); foreach ($new_paths as $path) { if ($path == '.' || $path == '..') { continue; } $path = $dir . DIRECTORY_SEPARATOR . $path; if (is_dir($path)) { $all_paths = array_merge($all_paths, scan_dir_recursive($path)); } else { $all_paths[] = $path; } } return $all_paths; } foreach($mirror as $mirr) { if($mirr != '.' &amp;&amp; $mirr != '..') { if(!in_array($mirr, $original)) { unlink($mirr); // delete the file } } } </code></pre> <p>The above code shows what i did.. Here My copy1 folder contains extra files than copy2 folders hence i need these extra files to be deleted.</p> <blockquote> <p><strong>EDITED:</strong> Below given output is are arrays of original Mirror and of difference of both..</p> </blockquote> <pre><code>Original Array ( [0] =&gt; /var/www/html/copy2/Copy (5) of New Text Document.txt [1] =&gt; /var/www/html/copy2/Copy of New Text Document.txt ) Mirror Array ( [0] =&gt; /var/www/html/copy1/Copy (2) of New Text Document.txt [1] =&gt; /var/www/html/copy1/Copy (3) of New Text Document.txt [2] =&gt; /var/www/html/copy1/Copy (5) of New Text Document.txt ) Difference Array ( [0] =&gt; /var/www/html/copy1/Copy (2) of New Text Document.txt [1] =&gt; /var/www/html/copy1/Copy (3) of New Text Document.txt [2] =&gt; /var/www/html/copy1/Copy (5) of New Text Document.txt ) </code></pre> <p>when i iterate a loop to delete on difference array all files has to be deleted as per displayed output.. how can i rectify this.. the loop for deletion is given below.</p> <pre><code>$dirs_to_delete = array(); foreach ($diff_path as $path) { if (is_dir($path)) { $dirs_to_delete[] = $path; } else { unlink($path); } } while ($dir = array_pop($dirs_to_delete)) { rmdir($dir); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload