Hi all,

I have a very large amount of files I need to update, so I thought the easiest way would be to do it with php.
Below is the current directory structure

www.domain.com/branch_finder/country/county/town/

I need to replace the existing index page in every "county" level directory inside the branch_finder folder with a page here:

www.domain.com/staff/cdir/templates/county/index.php

I am guessing I would have to run a script inside the branch_finder directory to identify each county page and replace it?

How would I go about doing this?

Thanks in advance.
Max.

Member Avatar for nileshgr

please explain your problem in detail

Member Avatar for nileshgr

Phew it took me almost 1 hr to write the php code for this problem. Find the file as attachment.

Don't alter anything in the code without knowing what you're doing.

Run the command once without any arguments to get the format of parameters that need to be passed.

NOTE: This will copy SOURCEFILE to county/index.php

wow! thanks for all your time!!

Iv had a look at the script and it looks exactly what i needed, but im finding it a bit difficult to execute. can you give me an example of how to run it? like where should it be in the site for it to replace the right files?

also instead of executing it through the command line id like to pass the variables through the _get array

thanks again!!!

Member Avatar for nileshgr

Here's a sample -

Directory structure-

htdocs/
htdocs/domain.com
htdocs/domain.com/branch_finder
htdocs/domain.com/branch_finder/country
htdocs/domain.com/branch_finder/country/county

Path of original file which will be copied to the destination - /tmp/index.php

Command -

php rpl.php /tmp/index.php /tmp/htdocs/

I can't help you much with the _GET part cause I don't know your requirements.

thanks for all your help buddy! I wanted the code to replace the county/index.php pages - sorry if i wasnt clear enough. I have modified your script to change these files instead. also if anybody else uses it i have commented out the "copy" command in the "replacePage" function for testing purposes before any files are over written.

also because i cant / dont know how to run pages through command prompt or whatever i changed the argv to _GET variables so just run the page like this:

www.example.com/rpl.php?s=/www/htdocs/domain/public_html/template.php&d=/www/htdocs/domain/public_html

Thanks again!!

<?php
// Warning this may be a very bad design. Please inform nilesh [at] itech7 [dot] com if you get a better substitue.

	print "Make a backup of your files before you do modifcations\n\n";

	print "This application will overwrite any existing file without warning!\n\n";

	print "For example if your directory structure is like this -\n\n";
		
	print "\t/htdocs/domain.com/branch_finder/country/county\n\n";

	print "\tand you want to replace county/index.php with your one's\n\n";

	print "Pass argument #1 as path to source file\n\n";

	print "Pass argument #2 as the directory in which you have subdirectories as domain.com, domain.net, domain.org\n\n";

	print "NOTE: This will copy SOURCEFILE to county/index.php\n\n";

	print "By Nilesh, Site & Server Administrator of www.itech7.com\n\n<br/><br/><br/><br/><br/><br/>";



$sourcefile = '/var/www/localhost/htdocs/sharedweb/nsci/staff/cdir/template/county/index.php';

$destdir = '/var/www/localhost/htdocs/sharedweb/nsci/';

$d = new DirectoryIterator($destdir);

foreach ($d as $f) {
	
	if($f->isDir() and !$f->isDot()) { // Level 1 (domain.com's parent)
		
		$d1 = new DirectoryIterator($f->getPath() . '/' . $f);

		foreach ($d1 as $f1) {

			if($f1->isDir() and !$f1->isDot()) { // Level 2 (branch_finder's parent)
			
				$d2 = new DirectoryIterator($f1->getPath() . '/' . $f1);

				foreach ($d2 as $f2) {

					if($f2->isDir() and !$f2->isDot()) { // Level 3 (country's parent)
				
						if( strstr( $f2->getPath().'/'.$f2,'branch_finder')){//if branch_finder directory tree
							replacePage($sourcefile, $f2->getPath() . '/' . $f2 . '/index.php');
						}

					}

				}

			}

		}
	
	}

}

function replacePage($pagePath, $rpdir) {
	
	if(file_exists($pagePath)) {

		print "$rpdir<br/>";

		//copy($pagePath, $rpdir);
	
	} else {
		
		die("File $pagePath does not exist");
	
	}
	
}
?>
Member Avatar for diafol

Just to throw in my twopenneth worth:

Why not just have one index page that has a querystring with country and county as parameters. You can then dynamically create the page on the fly with php/mysql and if you want, you can have your folder structure via htaccess mod rewrite (although the folders wouldn't actually exist).

www.domain.com/index.php?country=england&county=avon

would then look like:

www.domain.com/england/avon/

If you don't want to mess with a DB for content, you can have a 'content' folder that has some content include files:

england_avon.php
wales_ceredigion.php
england.php
wales.php

etc etc.

Your index.php page would have something like:

if(isset($_GET['country'])){
  $first = $_GET['country'];
  if(isset($_GET['county'])){
    $second = "_" . $_GET['county'];
  }else{
    $second = "";
  }
  $incfile = $_SERVER['DOCUMENT_ROOT'] . "/contents/" . $first . $second . ".php";
  include($incfile);
}

This way you don't need to create millions of folders - just another include file for that country/county.

Member Avatar for nileshgr

Just to throw in my twopenneth worth:

Why not just have one index page that has a querystring with country and county as parameters. You can then dynamically create the page on the fly with php/mysql and if you want, you can have your folder structure via htaccess mod rewrite (although the folders wouldn't actually exist).

www.domain.com/index.php?country=england&county=avon

would then look like:

www.domain.com/england/avon/

If you don't want to mess with a DB for content, you can have a 'content' folder that has some content include files:

england_avon.php
wales_ceredigion.php
england.php
wales.php

etc etc.

Your index.php page would have something like:

if(isset($_GET['country'])){
  $first = $_GET['country'];
  if(isset($_GET['county'])){
    $second = "_" . $_GET['county'];
  }else{
    $second = "";
  }
  $incfile = $_SERVER['DOCUMENT_ROOT'] . "/contents/" . $first . $second . ".php";
  include($incfile);
}

This way you don't need to create millions of folders - just another include file for that country/county.

There must be some reason behind that. Possibly each county manages its own folder ?

Member Avatar for diafol

There must be some reason behind that. Possibly each county manages its own folder ?

Quite possibly, but even so, the url rewrite can deal with any files in addition to "index.php". It just seems like an extreme filesystem for a relatively straightforward need. 'Include file' switching OR/AND db content should be able to cope with most requirements of this type. Setting up a new county and maintaining all these folders and subfolders must be a nightmare.

I'm sorry if I came across as dismissing the other approach, this was not my intention. The iteration solution, from what I can see, should 'meet the brief'.

I didnt actually know about the .htaccess rewrite function and didnt know that this kind of setup was an option.

It's great to learn new ways of doing things through but now I have this all setup and it is working fine, I'll use this system for now, but your way will certainly be useful for future projects.

Thanks ardav and itech for your help!!

Max

Member Avatar for diafol

No problem. itech's solution looks really good. Hope it works out for you.

yeah it works great. thanks again itech

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.