Hello
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 ?
Please explain how

Member Avatar for priesdelly

write in header php file :)

include("../script.php");
  1. mysite.com/folder/script.php
  2. mysite.com/folder/subfolder/testfile.php

    //testfile.php
    include("../script.php");
    //do something

can help you ?

I'm assuming you want the php script to act on the subfolders
The google search would be something like "php recursive script"
I have one that I use to make a folder tree of pdf files downloadable, it may provide an insight, it is probably badly written, but functions ok for my purposes

<?php 
function get_Extension($ff){ $path_parts = pathinfo($ff);
 if ($path_parts["extension"]) { $m_Extension = strtoupper($path_parts["extension"]); 
  return($m_Extension);}
 else return("unknown");}
function check_file($ff){$temp=get_Extension($ff);
 if(($temp=="PDF")) return (true);
 else return (false);}
function printQueue($queue, $path) { asort($queue); foreach($queue as $file)    { printFile($file, $path); } }
function printFile($file, $path) { if (check_file($file)) { echo '<li><a href="./savefile.php?file='.$path.$file.'">'.substr($file,0,strlen($file)-4).'</a></li>';}}
function printSubDir($dir, $path) { echo "<li>$dir"; 
 createDir($path.$dir."/"); 
 echo "</li>";}
function createDir($path = './') { if ($handle = opendir($path)){ echo "<div style='margin-bottom:1em;'><ul>";  
 $queue = array();
 while (false !== ($file = readdir($handle))) { if (is_dir($path.$file) && $file != '.' && $file !='..') { printSubDir($file, $path, $queue);}   
  elseif ($file != '.' && $file !='..') {$queue[] = $file;}} 
  printQueue($queue, $path);
  echo "</ul></div>"; }}
createDir(); ?>

savefile.php, referenced in the script is a force-file-download script not required for the recurse function

OK I ll try thanks

almostbob here is the php script that I want to run in each folder
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

Can you please help?

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

thinking about what to alter {begin jeopardy thinking music}

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.