【问题标题】:php duplicate a file in other foldersphp复制其他文件夹中的文件
【发布时间】:2012-08-17 01:41:19
【问题描述】:

有没有办法我可以使用 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);

【问题讨论】:

  • 这是一个奇怪的用例。你想达到什么目的?
  • 如果用户请求一个文件并且所有其他文件也应该执行,为什么不简单地包含它们呢?或者您只是想要这样:无论用户调用哪个 URL,执行的始终是同一个文件?比你应该看看mod_rewrite
  • 您也可以使用 .htaccess 文件将用户重新路由到正确的目录,即 /demos/ 重新路由到 /demo/ 或其他内容。作为另一种选择,因为我相信 mod_rewrite 可能是一个更好的解决方案。
  • @insertusernamehere 问题是我不希望用户访问其他子文件夹。用户只允许访问主 index.php 并且当用户访问主 index.php 然后在同时它也必须在所有文件夹中执行
  • 我认为您实际上正在考虑使用 mod_rewrite 使其看起来像是从您网站的不同位置运行的同一页面。

标签: php duplicates


【解决方案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 语句中弹出代码。

希望对您有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-06
    • 1970-01-01
    • 2016-02-28
    • 2014-02-15
    • 2021-05-17
    • 1970-01-01
    相关资源
    最近更新 更多