Simple image display with PHP using regex...

jch02140

New Member
Messages
18
Reaction score
0
Points
1
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

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.
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
PHP:
<?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 = 0 ;
$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 == 0 ){
        $count=0;
        $display .= "<br />";
     }
  }

}

// clean up
closedir( $dir_handle );

echo $display ; 

?>
 
Last edited:

elcrimer81

New Member
Messages
1
Reaction score
0
Points
0
PHP:
     $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.
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
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:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
If there aren't any files that match /[^a-z0-9]{4}_th.jpg/, you can also use glob to get the files:
PHP:
...
foreach (glob($image_dir_path . '/????_th.jpg') as $filename) {
   ...  
}

This does away with opendir, readdir and preg_match.
 

jch02140

New Member
Messages
18
Reaction score
0
Points
1
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:
 <?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 = 0 ;
$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 == 0 ){
        $count=0;
        $display .= "<br />";
     }
  }

}

// clean up
closedir( $dir_handle );

echo $display ; 

?>
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
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).
 
Top