0

Is there a way I could use php to make root file to look like its also in other folders too.

For example I have index.php in root folder and I want it to be like that when I access index.php then it could also behave as its in all the folders and subfolders too

When I execute index.php then it will also execute in all folders and subfolders too

Please understand my question by the example below

index.php is in root and I have different folders in root as well so when I access the index.php through browser then it will also execute in other folders

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 is not in these folders but it must execute in these folders too at the same time

I think its not difficult to understand through above examples.please answer accordingly

Update 2

Here is the index.php code

It scans the folders "files" "images" "txt" "related" and get the files in each folder and then it writes to the includes.php (in root)

$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);
  • 1
    This is a *bizarre* use-case. What are you trying to achieve? – MetalFrog Aug 21 '12 at 16:50
  • If the user requests one file and all other files should be executed as well, why not simply inlude them? Or do you just want that: No matter which URL the user calls, it's always the same file that gets executed? Than you should take a look into `mod_rewrite`. – insertusernamehere Aug 21 '12 at 16:54
  • You could also use .htaccess files to reroute the user to the correct directory, ie /demos/ reroutes to /demo/ or something. Just as another option, since I believe mod_rewrite, might be a better solution. – thatidiotguy Aug 21 '12 at 16:57
  • @insertusernamehere well the problem is I do not want the user to access other sub-folders.user is only allowed to access the main index.php and when user access the main index.php then at the same time it must execute in all folders too – user1613566 Aug 21 '12 at 17:14
  • I think your actually looking at using mod_rewrite to make it look like the same page runs from different locations on your website. – Sammaye Aug 21 '12 at 17:16
  • @thatidiotguy please see the above comment.mod_rewrite is not an option for me as it doesnot fullfills my requirements – user1613566 Aug 21 '12 at 17:16
  • @Sammaye mod_rewrite is not an option as the users will not access the subfolders,they only access the main index.php so when they do it will also execute on subfolders too – user1613566 Aug 21 '12 at 17:18
  • http://stackoverflow.com/questions/1340001/deny-direct-access-to-all-php-files-except-index-php That should get you started – Sammaye Aug 21 '12 at 17:18
  • @Sammaye this question has nothing to do with my question.I do not want to deny access to subfolders – user1613566 Aug 21 '12 at 17:20
  • what does index.php *do*? that might help trying to work out how it needs to run in the other directories. – Stu Aug 21 '12 at 17:22
  • `well the problem is I do not want the user to access other sub-folders.user is only allowed to access the main index.php` that question tells you how to do that, the duplication thing....well I have no idea why your doing that but you can use exec to cp – Sammaye Aug 21 '12 at 17:22
  • @Stu I have updated index.php code in my quesion – user1613566 Aug 21 '12 at 17:30
  • @Sammaye Yes user only access index.php but it also must execute in other folders too please see the example I have given in the question – user1613566 Aug 21 '12 at 17:31
  • could you not add the directories you want to scan into the index.php? i.e. `$path = array("./files/","./images/","./txt/","./related/","./folder1/files/","./folder1/images/","./folder1/txt/","./folder1/related/");`? – Stu Aug 21 '12 at 17:32
  • @Stu Ok good idea but I do not want to specify each folder so is there a way I could add a variable in the path so that when I create new folder then I do not have to include that folder,because there are 100s of folders – user1613566 Aug 21 '12 at 17:34
  • @user1613566 Yes it was supposed to solve ONE of your problems, I have no idea though what you mean by `execute`? You have gone from executing all php files to including all files in folders....wtf... – Sammaye Aug 21 '12 at 17:35
  • I supose you could cycle through all the directories and then see if the folders in the `$path` array exist there? – Stu Aug 21 '12 at 17:36
  • @Stu Yes can you tell me a way how I could cycle through all the dir ? – user1613566 Aug 21 '12 at 17:50

1 Answers1

1

If you need to cycle through the directories and see if each of those directories contains one of the directories listed in the $path array you could use something like:

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();

That will cycle through all the directories in the root folder and see if they contain a directory listed in the $path array, if so you can pop your code in the // do what you need to do statement.

Hope that helps!

Stu
  • 4,160
  • 24
  • 43