Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 23
  1. #1
    New Coder
    Join Date
    Jul 2012
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    get file name and image and writes to a file

    Hello,
    The following script writes to a file include.php
    Code:
        <?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);
        ?>
    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)

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

    Code:
        {
        levels: [
        { file: "path to/files/filename1" }
        ],
        title: "filename2"
        },
        {
        levels: [
        { file: "path to/files/filename2" }
        ],
        title: "filename2"
        },
    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" }

    So the result will be

    Code:
         {
        levels: [
        { file: "path to/files/filename1" }
        ],
        image: "path to/images/imagefilename1",
        title: "filename2"
        },
    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 ?

  • #2
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    738
    Thanks
    20
    Thanked 85 Times in 85 Posts
    will the image name in the image folder be the same as the filename in the files folder except for the extension?

  • #3
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    738
    Thanks
    20
    Thanked 85 Times in 85 Posts
    this will do multiple folders just add them into the arrays at the top

    PHP Code:
    <?php
    $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");

        foreach(
    $path as $key => $dir) {
        
    $folder opendir($dir);
        
    $filename strstr($file'.'true);
        while( 
    $file readdir($folder) ) {
               if ((
    $file != '.')&&($file != '..')&&($file != 'index.htm')) {
                
    $result="{\nlevels: [\n{ file: \"$path2[$key]$file\" }\n],\n\ntitle: \"$file\"\n},\n";
                
    fwrite($inF,$result);
               }
        }
        
    fwrite($inF,"");
        
    closedir($folder);
        }
        
    fclose($inF);
    ?>

  • #4
    New Coder
    Join Date
    Jul 2012
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Arcticwarrio View Post
    will the image name in the image folder be the same as the filename in the files folder except for the extension?
    Yes its the same

  • #5
    New Coder
    Join Date
    Jul 2012
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Arcticwarrio View Post
    this will do multiple folders just add them into the arrays at the top

    PHP Code:
    <?php
    $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");

        foreach(
    $path as $key => $dir) {
        
    $folder opendir($dir);
        
    $filename strstr($file'.'true);
        while( 
    $file readdir($folder) ) {
               if ((
    $file != '.')&&($file != '..')&&($file != 'index.htm')) {
                
    $result="{\nlevels: [\n{ file: \"$path2[$key]$file\" }\n],\n\ntitle: \"$file\"\n},\n";
                
    fwrite($inF,$result);
               }
        }
        
    fwrite($inF,"");
        
    closedir($folder);
        }
        
    fclose($inF);
    ?>
    The result is the same and error log shows
    Code:
    PHP Warning:  Wrong parameter count for strstr() in /index.php on line 11
    Line 11:
    Code:
    $filename = strstr($file, '.', true);

  • #6
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    738
    Thanks
    20
    Thanked 85 Times in 85 Posts
    just put an if in to see if the files names match

  • #7
    New Coder
    Join Date
    Jul 2012
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Arcticwarrio View Post
    just put an if in to see if the files names match
    you mean before line 11 ?

  • #8
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    738
    Thanks
    20
    Thanked 85 Times in 85 Posts
    you need to suppress warnings in your PHP ini file

    this should do what you want:

    PHP Code:
    <?php
    $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[1]);
        while( 
    $file readdir($folder) ) {
               if ((
    $file != '.')&&($file != '..')&&($file != 'index.htm')) {
                    
    $folder2 opendir($path[2]);
                    
    $imagename ='';
                    while( 
    $file2 readdir($folder2) ) {
                        if (
    strstr($file2'.'true) == strstr($file'.'true)){
                            
    $imagename $file2;
                        }
                    }
                
    $result="{\nlevels: [\n{ file: \"$path2[$key]$file\" }\n],\n\nimage: \"$imagename\"\n\ntitle: \"$file\"\n},\n";
                
    fwrite($inF,$result);
               }
        }
        
    fwrite($inF,"");
        
    closedir($folder);
        
    fclose($inF);
    ?>

  • #9
    New Coder
    Join Date
    Jul 2012
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts
    It gives the following result
    Code:
    {
    levels: [
    { file: "sintel_trailer3.jpg" }
    ],
    
    image: ""
    
    title: "sintel_trailer3.jpg"
    },
    {
    levels: [
    { file: "big_buck_bunny_trailer.jpg" }
    ],
    
    image: ""
    
    title: "big_buck_bunny_trailer.jpg"
    },
    image is empty and image file is written in file and title:
    It should be like
    Code:
    {
        levels: [
        { file: "path to/files/filename1" }
        ],
        image: "path to/images/imagefilename1",
        title: "filename2"
        },

  • #10
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    738
    Thanks
    20
    Thanked 85 Times in 85 Posts
    think i've fixed it now

    PHP Code:
    <?php
    $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 (
    strstr($file2'.'true) == strstr($file'.'true)){
                            
    $imagename $file2;
                        }
                    }
                    
    closedir($folder2);
                
    $result="{\nlevels: [\n{ file: \"$path2[$key]$file\" }\n],\nimage: \"$imagename\"\ntitle: \"$file\"\n},\n";
                
    fwrite($inF,$result);
               }
        }
        
    fwrite($inF,"");
        
    closedir($folder);
        
    fclose($inF);
    ?>
    gives me this:

    Code:
    {
    levels: [
    { file: "on.html" }
    ],
    image: "on.gif"
    title: "on.html"
    },
    Last edited by Arcticwarrio; 08-01-2012 at 11:35 AM. Reason: added output

  • #11
    New Coder
    Join Date
    Jul 2012
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Yes getting close now but the image file is same for evry other file
    in files folder I have two video files
    sintel_trailer1.mp4
    sintel_trailer2.mp4

    and in images folder I have two images
    sintel_trailer1.jpg
    sintel_trailer2.jpg

    and its giving me this result

    Code:
    {
    levels: [
    { file: "sintel_trailer1.mp4" }
    ],
    image: "sintel_trailer2.jpg"
    title: "sintel_trailer1.mp4"
    },
    {
    levels: [
    { file: "sintel_trailer2.mp4" }
    ],
    image: "sintel_trailer2.jpg"
    title: "sintel_trailer2.mp4"
    },
    The image file is same for every loop "sintel_trailer2.jpg"
    whereas file name is changing accordingly

  • #12
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    738
    Thanks
    20
    Thanked 85 Times in 85 Posts
    i made those files to test and i get

    Code:
    {
    levels: [
    { file: "sintel_trailer1.mp4" }
    ],
    image: "sintel_trailer1.gif"
    title: "sintel_trailer1.mp4"
    },
    {
    levels: [
    { file: "sintel_trailer2.mp4" }
    ],
    image: "sintel_trailer2.gif"
    title: "sintel_trailer2.mp4"
    },

  • #13
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    738
    Thanks
    20
    Thanked 85 Times in 85 Posts
    you could try changing

    PHP Code:
    $result="{\nlevels: [\n{ file: \"$path2[$key]$file\" }\n],\nimage: \"$imagename\"\ntitle: \"$file\"\n},\n";
                
    fwrite($inF,$result); 
    to:

    PHP Code:
    $result="{\nlevels: [\n{ file: \"$path2[$key]$file\" }\n],\nimage: \"$imagename\"\ntitle: \"$file\"\n},\n";
                
    $imagename ='';
                
    fwrite($inF,$result); 

  • #14
    New Coder
    Join Date
    Jul 2012
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Are you sure you are getting the exact result that I need ?

    because I changed the code you specified as well but still the same result

    what could be the issue ?
    my index.php(which contains the code) is not in the root
    root/dir/index.php
    root/dir/files/
    root/dir/images

    this is the structure

  • #15
    New Coder
    Join Date
    Jul 2012
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Can u please recheck the codes you tried and you pasted here ?
    I am getting the same result


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •