I don't think I am the only one who would like to create a file structure such as:
"country_name/country_name.html"
"country_name/state_name/state_name.html"
"country_name/state_name/county_name/county_name.html
"country_name/state_name/county_name/city_or_town_or_village_name/city_or_town_or_village_name.html"

I would like to do this for every country in the world. But the tediousness kills me. Is there a ready-made directory structure such as this available? Or a program or php script that would do this for me automatically from lists of countries, states, cities etc.? I used a program called "Folder Maker" which is great at creating single level folders or files but then i have to put each file in the correct folder manually. I could hire someone from Elance to do this for me but . . . any ideas or help would be appreciated.

    Do these need to be real files, or can you just use some magic (PHP, mod_rewrite, and friends) to make it appear that you have something set up like this.

    IIRC (and it's been a LOOOOONG time), but PHPBuilder founder Tim Perdue had an article on the site long ago telling how he'd done this with PHP and mod_rewrite for a site. I'll see if I can find the link; if nothing else, it might give you some idea how to proceed....

      Tim build "gotocity.com" back in the late 90's. Here's how he did it then. Note that the site is no longer "out there" and is currently a NetSol redirect/landing page.

        dalecosp;11035875 wrote:

        Tim build "gotocity.com" back in the late 90's. Here's how he did it then. Note that the site is no longer "out there" and is currently a NetSol redirect/landing page.

        Thanks, dalecosp. This will help, but I do need to have the "real" files available.

          If you have access to a linux machine, there is a command, mkdir, that takes a -p flag. Assuming you had some list of the directories and files you want to create (it could be in a database, in a data file, or programmatically generated somehow) then you could do something like this:

          <?php
          
          // you would need to define this array according to your own needs
          $dirs_to_make = array(
                  "dir1/subdir1",
                  "dir2/subdir2/subsubdir2",
                  "dir3/subdir3/subsubdir3/subsubsubdir3"
          );
          
          $cwd = dirname(__FILE__);
          
          foreach($dirs_to_make as $dir) {
                  $to_make = $cwd . DIRECTORY_SEPARATOR . $dir;
                  echo "making $to_make\n";
                  make_dir($to_make);
          }
          
          function make_dir($d) {
                  $mkdir_cmd = "mkdir -p " . $d;
                  $output = NULL; // will contain an array of output lines from the exec command
                  $result = NULL; // will contain the success/failure code returned by the OS.  Will be zero for a valid command, non-zero otherwise
                  $ret = exec($mkdir_cmd, $output, $result); // $ret will contain the last line from the result from the command
          
              if ($result) {
                      throw new Exception("Result was not zero, so something went wrong, result=" . $result . "\n" . print_r($output, TRUE));
              }
          }
          ?>
          

          VERY IMPORTANT: This code would not be secure for handling user input. It'd be very important for screening the directories to make sure no one is trying to overwrite /etc/passwd or some other sensitive file.

          If you are not running linux, then you'll need to write a slightly different function...something recursive probably.

            OK I didn't realize that mkdir supported the $recursive parameter. That looks helpful :bemused:

              Write a Reply...