1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

get file name and image and writes to a file

Discussion in 'PHP' started by ska_defender, Jul 31, 2012.

  1. #1
    Hello,
    The following script writes to a file include.php
        <?php
        $path = "./files/";
        $path2="http://".$_SERVER['SERVER_NAME'].dirname($_SERVER["PHP_SELF"])."/files/";
        //echo $path2;
        $folder = opendir($path);
        $start="";
        $Fnm = "./include.php";
        $inF = fopen($Fnm,"w");
        fwrite($inF,$start."\n");
        while( $file = readdir($folder) ) {
        if (($file != '.')&&($file != '..')&&($file != 'index.htm')) {
        $result="{\nlevels: [\n{ file: \"$path2$file\" }\n],\n\ntitle: \"$file\"\n},\n";
        fwrite($inF,$result);
        }
        }
        fwrite($inF,"");
        closedir($folder);
        fclose($inF);
        ?>
    Code (markup):
    It searches the folder and look for any file and store it in $path2$file
    so the result posted in include.php is (If the folder has one file)

        {
        levels: [
        { file: "path to/files/filename" }
        ],
        title: "filename"
        },
    Code (markup):
    and if the folder has 2 files result is

        {
        levels: [
        { file: "path to/files/filename1" }
        ],
        title: "filename2"
        },
        {
        levels: [
        { file: "path to/files/filename2" }
        ],
        title: "filename2"
        },
    Code (markup):
    and so on...

    What I need is to search for another folder /images and write the same image file in include.php just below: { file: "path to/files/filename1" }
    SEMrush
    So the result will be

         {
        levels: [
        { file: "path to/files/filename1" }
        ],
        image: "path to/images/imagefilename1",
        title: "filename2"
        },
    Code (markup):
    and it will post the same image path for every image file in images folder

    I have tried to incorporate the same code with slight variable changes but I am stuck at using while

    Any help ?
    I wont mind paying some bucks if anyone help me to solve this
     
    Solved! View solution.
    ska_defender, Jul 31, 2012 IP
    robertjhon likes this.
    SEMrush
  2. #2
    This should do the trick
    <?php    
    $checks = array('files','images');     
    foreach($checks as $path) writefilelist($path);
        
    
    function writefilelist($str) {    
       $path = "./{$str}/";
        $path2="http://".$_SERVER['SERVER_NAME'].dirname($_SERVER["PHP_SELF"])."/files/";
        //echo $path2;
        $folder = opendir($path);
        $start="";
        $Fnm = "./include.php";
        $inF = fopen($Fnm,"w");
        fwrite($inF,$start."\n");
        while( $file = readdir($folder) ) {
          if (($file != '.')&&($file != '..')&&($file != 'index.htm')) {
             $result="{\nlevels: [\n{ file: \"$path2$file\" }\n],\n\ntitle: \"$file\"\n},\n";    fwrite($inF,$result);
           }
        }
        fwrite($inF,"");
        closedir($folder);
        fclose($inF);
    }
    ?>
     
    sarahk, Aug 1, 2012 IP