Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get a directory content and move to another directory with PHP
    text
    copied!<p>I've been trying to get this done with no luck. I've got a php script that downloads and UNZIPS a file. No problem here.</p> <p>After that, I need to get the unzipped files and replace all the files of the root directory with the files on this folder. Problem is, the folder is in the root directory.</p> <p>Eg.: my directory is something like this</p> <pre><code>/ /folder 1 /folder 2 /folder 3 /file.php /file2.php etc... </code></pre> <p>After I run my download script I unzip the archive to a folder called update in the root of the dir</p> <pre><code>/update/--UNZIPED STUFF-- </code></pre> <p>I need to delete virtually all files/dirs from the root DIR excluding the update folder I guess and a couple of other files and then move the files/folders inside the update folder to the root of the dir.</p> <p>Any ideas?</p> <p><em><strong>UPDATE</em></strong></p> <p>Ok, so I've made a code that seems to be right but I get a lot of file permission to when using unlink or rename. How can I go about that?</p> <p>Here is the bit of code I've made</p> <pre><code> echo '&lt;table&gt;'; $updateContents = scandir('./'); foreach($updateContents as $number=&gt;$fileName){ if($fileName != 'update' &amp;&amp; $fileName != 'config.ini' &amp;&amp; $fileName != 'layout.php' &amp;&amp; $fileName != '..' &amp;&amp; $fileName != '.' &amp;&amp; $fileName != '.git' &amp;&amp; $fileName != '.gitignore'){ if(is_dir($fileName)){ moveDownload($fileName, 'old/'.$fileName); } else { if(rename($fileName, 'old/'.$fileName)){ echo '&lt;tr&gt;&lt;td&gt;'.$fileName.' moved successfully &lt;/td&gt;&lt;td&gt;&lt;font color="green"&gt;OK&lt;/font&gt;&lt;/td&gt;&lt;/tr&gt;'; } else { echo '&lt;tr&gt;&lt;td&gt;Could not move file '.$fileName.'&lt;/td&gt;&lt;td&gt;&lt;font color="red"&gt;ERROR&lt;/font&gt;&lt;/td&gt;&lt;/tr&gt;'; } } } } echo '&lt;/table&gt;'; </code></pre> <p>and the moveDownload function is one I found here somewhere. It moves a non empty folder and it was slightly modified by me.</p> <pre><code>function moveDownload($src,$dst){ $dir = opendir($src); while(false !== ($file = readdir($dir))){ if (($file != '.') &amp;&amp; ($file != '..') &amp;&amp; ($file != 'config.ini') &amp;&amp; ($file != 'layout.php') &amp;&amp; ($file != 'sbpcache') &amp;&amp; ($file !='update')){ if (is_dir($src.'/'.$file)){ if(file_exists($dst.'/'.$file)){ rrmdir($dst.'/'.$file); } } if(@rename($src . '/' . $file, $dst . '/' . $file)){ echo '&lt;tr&gt;&lt;td&gt;'.$file.' moved successfully &lt;/td&gt;&lt;td&gt;&lt;font color="green"&gt;OK&lt;/font&gt;&lt;/td&gt;&lt;/tr&gt;'; } else { if(@chmod($src.'/'.$file, 0777)){ if(@rename($src . '/' . $file, $dst . '/' . $file)){ echo '&lt;tr&gt;&lt;td&gt;'.$file.' moved successfully &lt;/td&gt;&lt;td&gt;&lt;font color="green"&gt;OK&lt;/font&gt;&lt;/td&gt;&lt;/tr&gt;'; } else { echo '&lt;tr&gt;&lt;td&gt;Could not move file '.$file.'&lt;/td&gt;&lt;td&gt;&lt;font color="red"&gt;ERROR RENAME&lt;/font&gt;&lt;/td&gt;&lt;/tr&gt;'; } } else { echo '&lt;tr&gt;&lt;td&gt;Could not move file '.$file.'&lt;/td&gt;&lt;td&gt;&lt;font color="red"&gt;ERROR CHMOD&lt;/font&gt;&lt;/td&gt;&lt;/tr&gt;'; } } } } closedir($dir); } </code></pre> <p>I also do the same in the again but moving from the update folder.</p> <p>So basically, first instead of deleting things I am just moving them to a folder called old and then I try to move the new things in, but I get permission problems everywhere. Some files move, some don't, even if I have ' sudo chmod 777 -R'. Any ideas?</p>
 

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