+ Reply to Thread
Results 1 to 7 of 7
Like Tree2Likes
  • 1 Post By jch02140
  • 1 Post By misson

Thread: Simple image display with PHP using regex...

  1. #1
    jch02140 is offline x10Hosting Member jch02140 is an unknown quantity at this point
    Join Date
    May 2008
    Posts
    12

    Question Simple image display with PHP using regex...

    Hi,

    I have upload a lot of images with filenames such as:

    • vvvv_th.jpg
    • airr_th.jpg
    • 4vor_th.jpg
    • mo40_th.jpg
    ...

    basically the filenames is represent in regular expression is

    http://www.domain.com/path/[a-z0-9]{4}_th.jpg
    I am trying to do a simple display script of 10 per row. However, I am not sure how to do it with PHP... if someone can help me out would be greatly appreciated.
    dinomirt96 likes this.

  2. #2
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: Simple image display with PHP using regex...

    PHP Code:
    <?php

     
    // split the path in two since I use the second half later
    $doc_root '/home/cPanelUsername/public_html' 
    $image_dir '/images/goats/' ;

    $image_dir_path $doc_root $image_dir  ;

    // open a handle to the directory
    $dir_handle opendir$image_dir_path );  

    // initialize counter and the display string
    $count ;
    $display '' ;

    // read one file name at a time
    while( $filename readdir($dir_handle)){
      
    // just the files that match your pattern
      
    if( preg_match'/$[a-z0-9]{4}_th\.jpg$/' $filename ) ){
        
    // add to display string...can add 'Alt' tag 
         
    $display .= " <img src='$image_dir$filename' /> " ;
        
         
    $count++ ;  
         
    // every 10 images, reset counter and add <br /> tag
         
    if( $count 10 == ){
            
    $count=0;
            
    $display .= "<br />";
         }
      }

    }

    // clean up
    closedir$dir_handle );

    echo 
    $display 

    ?>
    Last edited by descalzo; 09-12-2010 at 01:57 PM.
    Nothing is always absolutely so.

  3. #3
    elcrimer81 is offline x10Hosting Member elcrimer81 is an unknown quantity at this point
    Join Date
    Sep 2010
    Posts
    1

    Re: Simple image display with PHP using regex...

    Quote Originally Posted by descalzo View Post
    PHP Code:

         $display 
    .= " <img src='$image_dir$filename' /> " ;

    ?> 
    1-u can't use vars inside single-quotes
    instead close the double-quotes and use the append operator "."
    2-specify the image's width or use divs to display the images.

  4. #4
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: Simple image display with PHP using regex...

    Quote Originally Posted by elcrimer81 View Post
    1-u can't use vars inside single-quotes
    instead close the double-quotes and use the append operator "."
    2-specify the image's width or use divs to display the images.
    1. The variable is ultimately inside double quotes, that is all that matters. The code works.

    2. If all the images have the same width, fine. Your can hard code it. Otherwise you need more coding to find the width. For a 'simple display script', I did not think it was worth the time.
    As for <div>, I just gave a quick outline of the script. He can fiddle with HTML and maybe style sheets later.
    Last edited by descalzo; 09-12-2010 at 02:15 PM.
    Nothing is always absolutely so.

  5. #5
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Simple image display with PHP using regex...

    If there aren't any files that match /[^a-z0-9]{4}_th.jpg/, you can also use glob to get the files:
    PHP Code:
    ...
    foreach (
    glob($image_dir_path '/????_th.jpg') as $filename) {
       ...  

    This does away with opendir, readdir and preg_match.
    karimirt47 likes this.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  6. #6
    jch02140 is offline x10Hosting Member jch02140 is an unknown quantity at this point
    Join Date
    May 2008
    Posts
    12

    Question Re: Simple image display with PHP using regex...

    Thanks for all the help

    Just curious, if I am to put this script in a different server would the script be something like this?

    PHP Code:
     <?php

     
    // split the path in two since I use the second half later
    $image_dir '<remote server>' ;

    // open a handle to the directory
    $dir_handle opendir$image_dir );  

    // initialize counter and the display string
    $count ;
    $display '' ;

    // read one file name at a time
    while( $filename readdir($dir_handle)){
      
    // just the files that match your pattern
      
    if( preg_match'/$[a-z0-9]{4}_th\.jpg$/' $filename ) ){
        
    // add to display string...can add 'Alt' tag 
         
    $display .= " <img src='$image_dir$filename' /> " ;
        
         
    $count++ ;  
         
    // every 10 images, reset counter and add <br /> tag
         
    if( $count 10 == ){
            
    $count=0;
            
    $display .= "<br />";
         }
      }

    }

    // clean up
    closedir$dir_handle );

    echo 
    $display 

    ?>

  7. #7
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Simple image display with PHP using regex...

    Only if you use an FTP URL and allow_url_fopen is enabled. Filesystem functions can only access objects that are, well, accessible via the filesystem. You wouldn't expect opendir to work with databases, would you? Fortunately for you, PHP's opendir has been extended to work with FTP. You could add support for HTTP URLs by writing your own stream wrapper, but it would only work if the resource named by the URL had a well-defined format, such as remote directorys when the server has directory listing enabled or if the server supported WebDAV or AtomPUB.

    Other than that, you'd need a script on the remote server to generate a directory listing and send it in a data interchange format (e.g. JSON or XML).
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. Simple Image RSS?
    By aliasneo in forum Scripts & 3rd Party Apps
    Replies: 0
    Last Post: 05-11-2010, 06:01 PM
  2. Display image in MYSQL
    By acellec in forum Programming Help
    Replies: 27
    Last Post: 06-22-2009, 03:27 PM
  3. Simple htaccess RegEx
    By nterror in forum Programming Help
    Replies: 5
    Last Post: 05-30-2009, 09:33 AM
  4. Simple Image
    By jtaah in forum Free Hosting
    Replies: 0
    Last Post: 08-16-2008, 06:17 AM
  5. php image display
    By stalkio in forum Programming Help
    Replies: 3
    Last Post: 08-10-2008, 06:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers