Hi Guys,

i've been cracking my brain over something that should probably be so simple..

I try to build a small image gallery where the thumbnails and large files are in the same directory. The names are quite simpe catsxx.jpg and catsxx-th.jpg (-th for the thumbnails). Know i try to do a listing of all files containing -th in the filename with the highest xx nr first, but have really no idea how to do this. Than i want to have a <ahref=""> linking the catsxx-th.jpg to the catsxx.jpg file. Could anyone please point me to the right direction how to just show/count the thumbnails and how i'm able to link them to the large files?

Much appreciated,

Questor

    For creating smaller images check here:
    http://us2.php.net/manual/en/function.imagecopyresized.php

    For getting a list of files in a directory:
    http://us2.php.net/manual/en/function.readdir.php

    That should give you a good heads up on writing this.

    Here is an example of taking a list of files in a directory removing the extension of the filename, use the filename as the link name and generating a list of links to the files.

    		$dh = opendir($dirname);
    
    while(!(($file = readdir($dh))===false)){
    	if(is_dir("$dirname/$file"))
    		;
    	else
    	{
    		$file_array = explode(".", $file);
    		// Code Enters in as link_name, file_name and repeats
    		$file_list[] = $file_array[0];
    		$file_list[] = $file;
    	}
    }
    
    
    echo"<p>";
    if($file_list[0]==NULL)
    	echo"No Files Found";
    
    for($f=1, $n=0; $file_list[$f] != ""; $f=$f+2, $n=$n+2)
    {
    	echo"&#8226; 
    		 <a href=\"$dirname/".$file_list[$n+1]."\" target=\"_blank\">".$file_list[$n]."</a>
    		 <br>";
    }
    
    echo"</p>";
    
    

      Hi nobleclem, thanks for your reply.

      Basicly i already created something that looks like your code, but here's the thing, i want to do a readdir for -th.jpg and than link the -th.jpg.

      I was thinking a little bit later that it otherwise be possible to do a count. So lets say i have a directory with 20 files (10 thumbs/10 large images), i count the nr of files and devide them by 2 (that would be 10) then i would create 10 links where the name of the file always is the same, only the nr of the file changes. Is something like this perhaps easier?

        If they are all going to be the same extenstion then you could use something like this:

        $len = strlen($filename);
        $compare = strcmp(substr($filename,$len - 7, $len), "-th.jpg");
        

        Check out:
        http://us3.php.net/manual/en/function.strcmp.php
        for the strcmp responses;

        If not then you could use something like this:

        $len = strlen($filename);
        $compare = strcmp(substr($filename,$len - 7, $len-4), "-th");
        

        Hope this helps out a little bit.

          I feel like such an idiot...

          I have no idea where to place it, or better yet how to do it. Right now i have the following code:

          <?
          $dirname = "/mnt/storage2/r/re/renateverhagen.nl/html/test/";

          $dh = opendir($dirname);

          while(!(($file = readdir($dh))===false)){ 
              if(is_dir("$dirname/$file")) 
                  ; 
              else 
              { 
                  $file_array = explode(".", $file); 
                  // Code Enters in as link_name, file_name and repeats 
                  $file_list[] = $file_array[0]; 
                  $file_list[] = $file; 
              } 
          } 

          echo"<p>";

          if($file_list[0]==NULL)
          echo"No Files Found";

          for($f=1, $n=0; $file_list[$f] != ""; $f=$f+2, $n=$n+2) 
          { 
              echo"&#8226; 
                   <a href=\"test/".$file_list[$n+1]."\" target=\"_blank\">".$file_list[$n]."</a> 
                   <br>"; 
          } 

          echo"</p>";
          ?>

          it shows al files instead of only the -th.jpg files. (output: http://www.renateverhagen.nl/test5.php)

          Could you help me how to show only the -th.jpg files and linking them to the big ones?

            <? 
            $dirname = "/mnt/storage2/r/re/renateverhagen.nl/html/test/"; 
            
            $dh = opendir($dirname); 
            
            while(!(($file = readdir($dh))===false)){ 
            if(is_dir("$dirname/$file")) 
            ; 
            else 
            { 
            $file_array = explode(".", $file); 
            // Code Enters in as link_name, file_name and repeats 
            
            // CODE CHANGE HERE
            $len = strlen($filename); 
            $compare = strcmp(substr($filename,$len - 7, $len-4), "-th");
            if(!$compare){
            $file_list[] = $file_array[0]; 
            $file_list[] = $file; 
            }
            } 
            } 
            // END CODE CHANGE
            
            echo"<p>"; 
            
            if($file_list[0]==NULL) 
            echo"No Files Found"; 
            
            for($f=1, $n=0; $file_list[$f] != ""; $f=$f+2, $n=$n+2) 
            { 
            echo""&#8226;
            <a href=\"test/".$file_list[$n+1]."\" target=\"_blank\">".$file_list[$n]."</a> 
            <br>"; 
            } 
            
            echo"</p>"; 
            ?> 
            

            Let me know how this works for you.

              Hmmz it gave me a parse error so i changed the last lines into:

              echo"•
              <a href=\"test/".$file_list[$n+1]."\" target=\"_blank\">".$file_list[$n]."</a>
              <br>";

              know the error is gone but it shows me No Files Found.

                <?PHP 
                $dirname = "/mnt/storage2/r/re/renateverhagen.nl/html/test/";  
                //$dirname = "images"; $dh = opendir($dirname); while(!(($file = readdir($dh))===false)){
                if(is_dir("$dirname/$file"))
                ;
                else
                {
                $file_array = explode(".", $file);
                // Code Enters in as link_name, file_name and repeats $len = strlen($file_array[0]); $compare = strcmp(substr($file_array[0],$len - 3, $len), "-th"); //MODIFIED if(!$compare){ $file_list[] = $file_array[0]; //filename with no extenstion $file_list[] = $file; //File } }
                } echo"<p>"; if($file_list[0]==NULL)
                echo"No Files Found"; for($f=1, $n=0; $file_list[$f] != ""; $f=$f+2, $n=$n+2)
                { // NEXT TWO LINES MODIFIED echo" <a href=\"$dirname/".str_replace('-th','',$file_list[$n+1])."\" target=\"_blank\"><img src=\"$dirname/".$file_list[$n+1]."\"></a> <br>";
                } echo"</p>";
                ?>

                Ok I tested this one out... this should work.
                Now you can always get fancy with the output too.

                  Cool, yes this is what i wanted. Is it possible to use ksort with this aswel so showing the most recent image first (highest nr first)?

                     <?PHP  
                    $dirname = "/mnt/storage2/r/re/renateverhagen.nl/html/test/";
                    //$dirname = "images"; $dh = opendir($dirname); while(!(($file = readdir($dh))===false)){
                    if(is_dir("$dirname/$file"))
                    ;
                    else
                    {
                    $file_array = explode(".", $file);
                    // Code Enters in as link_name, file_name and repeats $len = strlen($file_array[0]); $compare = strcmp(substr($file_array[0],$len - 3, $len), "-th"); if(!$compare){
                    //$file_list[] = $file_array[0]; //filename with no extenstion $file_list[] = $file; //File }
                    }
                    } ksort($file_list); reset($file_list) echo"<p>"; if($file_list[0]==NULL)
                    echo"No Files Found"; for($f=1, $n=0; $file_list[$f] != ""; $f=$f+2, $n=$n+2)
                    { echo"
                    <a href=\"$dirname/".str_replace('-th','',$file_list[$])."\" target=\"_blank\"><img src=\"$dirname/".$file_list[$n]."\"></a> <br>";
                    } echo"</p>";
                    ?>

                    Well I just noticed with this output we dont need to store the name of the file so I commented out that line and added the ksort, reset and modifed the output arrays.

                    Try it... see if thats what you want to do.

                    Oh and the directory has to be relative to the php file. So if you dont want it to be a relative location you will have to modify the output.

                      Hmmmz weird... nothing seems to happen, just a blanc html file without any result. Tried to see what changed between the two listings but nothing seems to work. Even tried entering the ksort in the previous listing but that had no result what so ever. Any idea?

                        <?PHP   
                        //$dirname = "/mnt/storage2/r/re/renateverhagen.nl/html/test/";
                        $dirname = "images"; $dh = opendir($dirname); while(!(($file = readdir($dh))===false)){
                        if(is_dir("$dirname/$file"))
                        ;
                        else
                        {
                        $file_array = explode(".", $file);
                        // Code Enters in as link_name, file_name and repeats $len = strlen($file_array[0]);
                        $compare = strcmp(substr($file_array[0],$len - 3, $len), "-th");
                        if(!$compare){
                        //$file_list[] = $file_array[0]; //filename with no extenstion $file_list[] = $file; //File }
                        }
                        } ksort($file_list); reset($file_list); echo"<p>"; if($file_list[0]==NULL)
                        echo"No Files Found"; for($n=0; $file_list[$n] != NULL; $n++)
                        {
                        echo"
                        <a href=\"$dirname/".str_replace('-th','',$file_list[$n])."\" target=\"_blank\"><img src=\"$dirname/".$file_list[$n]."\"></a> <br>";
                        } echo"</p>";
                        ?>

                        Here you go... Sorry...

                          Okay, i think i now see what went wrong, but i wonder if the sorting goes correctly. I alterd ksort in krsort (hoping this would do it in reverse order), but for some reason it still doesn't show the the nr of files from 20 to 1. (example: http://www.renateverhagen.nl/test5.php) Could it be that ksort looks on filedate and not filename?

                            Try rsort,

                            also if that doenst give what you want check this out:
                            http://us3.php.net/manual/en/function.sort.php

                            Look at the different sorts ... but I should have realized that ksort wasnt gonna do exactly what you want because that is more for if you have an array like

                            array(
                              'a' => '1',
                              'c' => '4',
                              'b' => '2',
                              'd' => '3'
                            );
                            

                            Now if you used ksort on this it would return

                            a,b,c,d

                            If you used sort it would return

                            a,b,d,c

                            Hope that helps..

                              Cool, i had to change the names of the images (01,02,03 instead of 1,2,3) but than it defenately did the trick 🙂)

                              May i thank you for all your help, now can use this with an upload script so that someone only needs to use an upload page to update the site.

                              Once again thanks for all your help, you really helped me alot.

                              Questor

                                Write a Reply...