0

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

例如,我在根文件夹中有 index.php,我希望它在我访问 index.php 时也能像它在所有文件夹和子文件夹中的行为一样

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

请通过以下示例了解我的问题

index.php 在根目录中,我在根目录中也有不同的文件夹,所以当我通过浏览器访问 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);
4

1 回答 1

1

如果您需要循环浏览目录并查看每个目录是否包含$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语句中弹出代码。

希望有帮助!

于 2012-08-21T18:08:09.263 回答