1

I have a php script that resides in a single folder I want to run the same script in each folder wihout manually uploading the php file in each file

for example I have mysite.com/folder/script.php

and folder has different subfolders

so I want to create a php file that will execute this script.php in each folder/subfolder without manually uploading the script.php in each folder

Is there a way ?

Update php code

$path = array("./files/","./images/");
$path2=  array("http://".$_SERVER['SERVER_NAME'].dirname($_SERVER["PHP_SELF"])."/files/","http://".$ _SERVER['SERVER_NAME'].dirname($_SERVER["PHP_SELF"])."/images/");
$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]);
            $imagename ='';
            while( $file2 = readdir($folder2) ) {
                if (substr($file2,0,strpos($file2,'.')) == substr($file,0,strpos($file,'.'))){
                    $imagename = $file2;
                }
            }
            closedir($folder2);
        $result="{\nlevels: [\n{ file: \"$path2[0]$file\" }\n],\nimage: \"$path2[1]$imagename\",\ntitle: \"$file\"\n},\n";
        fwrite($inF,$result);
       }
}
fwrite($inF,"");
closedir($folder);

fclose($inF);
user1561466
  • 71
  • 1
  • 3
  • 11

2 Answers2

4

Assuming you are using Apache, you could use mod_rewrite by creating a htaccess file that looks like this:

RewriteEngine on
RewriteBase /folder/
RewriteRule  ^.+/script.php$ script.php

And uploading it to your server. This turns the RewriteEngine on, sets the base directory to your chosen folder, then whenever script.php is requested in a subdirectory it will return /folder/script.php

bountiful
  • 814
  • 1
  • 8
  • 22
  • Can u please look at my updated php code it generates include.php file in root.But I want to generate that file in each subfolder – user1561466 Aug 19 '12 at 12:38
  • You aren't really being clear. This seems like a bit of a difficult way to go about it, but if you insist on using this method you can just replace `script.php` with `include.php` in `htaccess` – bountiful Aug 19 '12 at 12:47
  • include.php is not the actual file (its generated automatically by script.php).Actually the script scans the folder and then append it in include.php file which is created automatically so what I want to automatically create that include.php file for each folder.The code I have updated above is for script.php not include.php – user1561466 Aug 19 '12 at 13:18
  • In that case you want to run a `foreach` loop over `glob`. See this [this question](http://stackoverflow.com/questions/2524151/php-get-all-subdirectories-of-a-given-directory) – bountiful Aug 19 '12 at 13:34
2

On your subfolder's page, include this code:

require_once dirname(__FILE__) . "/folder/script.php";

Is this what you mean?

mrjimoy_05
  • 3,452
  • 9
  • 58
  • 95
  • NO there is no page in my subfolder(only contains images) I do not want to manaully edit the subfolder pages need to run a script that scans the folder and run scrip.php in every folder – user1561466 Aug 19 '12 at 09:56
  • What do you intend to do? Accessing the content on the subfolder from the `script.php`? – mrjimoy_05 Aug 19 '12 at 10:00
  • I have update the post to include my php scriot that I want to run.Actually the script scans the folder and then append it in include.php file which is created automatically so what I want to automatically create that include.php file for each folder – user1561466 Aug 19 '12 at 12:37