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

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

4 Comments

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
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
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
In that case you want to run a foreach loop over glob. See this this question
2

On your subfolder's page, include this code:

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

Is this what you mean?

3 Comments

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
What do you intend to do? Accessing the content on the subfolder from the script.php?
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.