php在其他文件夹中复制一个文件


php duplicate a file in other folders

有没有一种方法可以使用php使根文件在其他文件夹中也看起来像它。

例如,我在根文件夹中有index.php,我希望它是这样的,当我访问index.php时,它也可以在所有文件夹和子文件夹中表现为

当我执行index.php时,它也会在所有文件夹和子文件夹中执行

请通过下面的例子理解我的问题

index.php在root中,我在root中也有不同的文件夹,所以当我通过浏览器访问index.php时,它也会在其他文件夹中执行

http://mysite.com/index.php will also behave as if its in sub folder too
http://mysite.com/folder1/index.php
http://mysite.com/folder2/index.php
http://mysite.com/folder3/index.php

index.php不在这些文件夹中,但它必须同时在这些文件夹执行

我认为通过上面的例子不难理解。请相应地回答

更新2

这是index.php代码

它扫描文件夹"files"images"txt"related"并获取每个文件夹中的文件,然后写入includes.php(在根目录中)

$path = array("./files/","./images/","./txt/","./related/");
$path2= array("http://".$_SERVER['SERVER_NAME'].dirname($_SERVER["PHP_SELF"])."/files/","http://".$_SERVER['SERVER_NAME'].dirname($_SERVER["PHP_SELF"])."/images/","http://".$_SERVER['SERVER_NAME'].dirname($_SERVER["PHP_SELF"])."/txt/","http://".$_SERVER['SERVER_NAME'].dirname($_SERVER["PHP_SELF"])."/related/");
$start="";
$Fnm = "./include.php";
$inF = fopen($Fnm,"w");
fwrite($inF,$start."'n");

    $folder = opendir($path[0]);
    while( $file = readdir($folder) ) {
           if (($file != '.')&&($file != '..')&&($file != 'index.htm')) {
                $folder2 = opendir($path[1]);
                $folder3 = opendir($path[2]);
                $folder4 = opendir($path[3]);
                $imagename ='';
                $txtname ='';
                $related ='';
                while( $file2 = readdir($folder2) ) {
                    if (substr($file2,0,strpos($file2,'.')) == substr($file,0,strpos($file,'.'))){
                        $imagename = $file2;
                    }
                }
while( $file4 = readdir($folder4) ) {
                    if (substr($file4,0,strpos($file4,'.')) == substr($file,0,strpos($file,'.'))){
                        $related = $file4;
                    }
                }

while( $file3 = readdir($folder3) ) {

if (substr($file3,0,strpos($file3,'.')) == substr($file,0,strpos($file,'.'))){

                        $txtname = $file3;

                        $fh = fopen("/home3/socialb8/public_html/mysite.info/player/txt/$txtname", 'r');
                        $theData = fread($fh, filesize("/home3/socialb8/public_html/mysite.info/player/txt/$txtname"));
                        fclose($fh);

                    }

                }
                closedir($folder2);
                closedir($folder3);
                closedir($folder4);
            $result="{'nlevels: ['n{ file: '"$path2[0]$file'" }'n],'nimage: '"$path2[1]$imagename'",'ntitle: '"$file'",'ndescription: '"$theData'",'n 'related.file':'$path2[3]$related''n},'n";
            fwrite($inF,$result);
           }
    }
    fwrite($inF,"");
    closedir($folder);

fclose($inF);

如果您需要循环浏览这些目录,并查看这些目录中的每个目录是否包含$path数组中列出的一个目录,您可以使用以下内容:

function readDirs()
{
    $path=array('images','etc...');
    $dirHandle = opendir('./');
    while($file = readdir($dirHandle))
    {
        if(is_dir($file) && $file != '.' && $file != '..')
        {
            $dirHandle2 = opendir($file);
            while($file2 = readdir($dirHandle2))
            {
                if(in_array($file2,$path))
                {
                    // do what you need to do
                }
            }
        }
    }
}
readDirs();

这将遍历根文件夹中的所有目录,并查看它们是否包含$path数组中列出的目录,如果是,则可以在// do what you need to do语句中弹出代码。

希望能有所帮助!