PHP directory mirroring
<p>I want to mirror a directory through php so i can the files without having to directly modify them. </p>

<p>I found this script (<a href="http://lixlpixel.org/php_mirror/" rel="nofollow">here</a>) but i keep getting a 500 Internal Server Error when i try to set it up.</p>

<p>Could it be because its from 2004?</p>

<p>Are there any other ways to set this up or another method of doing this same thing?</p>

<p>here is some of the code i was using on a shared hosting plan</p>

<p>Agent file</p>

<pre><code>&lt;?php

error_reporting(0);

set_time_limit(0);

function recur_dir($dir)
{
    $dirlist = opendir($dir);
    while ($file = readdir ($dirlist))
    {
        if ($file != '.' &amp;&amp; $file != '..' &amp;&amp; $file != '.DS_Store' &amp;&amp; $file != 'mirror_agent.php')
        {
            $newpath = $dir.'/'.$file;
            $permissions = substr(decoct(fileperms($newpath)),-4);
            if (is_dir($newpath))
            {
                echo '
dir|'.$newpath.'|'.$permissions;
                recur_dir($newpath);
            }else{
                echo '
file|'.$newpath.'|'.$permissions.'
'.bin2hex(file_get_contents($newpath));
           }
        }
    }
    closedir($dirlist); 
    return $mod_array;
}

header('Content-type text/plain');
echo 'mirror_agent here';
recur_dir('.');

unlink(__file__);

?&gt;
</code></pre>

<p>Client File</p>

<pre><code>&lt;?php

$mirror_agent = 'http://moddline.com/safe/mirror_agent.php';

function hex2asc($temp)
{
    for ($i = 0; $i &lt; strlen($temp); $i += 2) $data .= chr(hexdec(substr($temp,$i,2)));
    return $data;
}

function file_write($filename, $filecontent, $mode='wb')
{
    if($fp = fopen($filename,$mode))
    {
        fwrite($fp, $filecontent);
        fclose($fp);
        return true;
    }else{
        return false;
    }
}

error_reporting(0);

set_time_limit(0);

$cont = file_get_contents($mirror_agent);
$files = explode("\n",$cont);

if(trim($files[0]) == 'mirror_agent here' &amp;&amp; is_writeable(dirname(__file__)))
{

    for($i = 1; $i &lt; count($files); $i++)
    {
        if(trim($files[$i]) != '')
        {
            $parts = explode('|',trim($files[$i]));
            $permissions = intval($parts[2], 8);
            if($parts[0] == 'dir')
            {

                mkdir($parts[1],$permissions);
                $done[] = $permissions.'|'.$parts[1];
            }elseif($parts[0] == 'file')
            {
                file_write($parts[1], hex2asc($files[$i+1]));
                chmod($parts[1],$permissions);
                $done[] = $permissions.'|'.$parts[1];
                $i++;
            }
        }
    }
    // output status
    echo '
&lt;pre&gt;
following files were copied:';

    for($i = 0; $i &lt; count($done); $i++)
    {
        echo '
'.$done[$i];
    }

    echo '
&lt;/pre&gt;';

    unlink(__file__);
}else{
    echo '
&lt;pre&gt;
ERROR:
'.$mirror_agent.'
does not respond like a mirror_agent should or
the directory '.dirname(__file__).' is not writeable.
&lt;/pre&gt;';
}

?&gt;
</code></pre>
