SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Wizard bronze trophy Tailslide's Avatar
    Join Date
    Feb 2006
    Location
    Bedford, UK
    Posts
    1,687
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Display files with certain prefix

    Hi everyone.

    Haven't been around for a while but hopefully back now with lots more PHP questions!

    How would I open a directory and display files (images) with a certain prefix? All files are prefixed by an id number e.g.: 5-myfilename.jpg or 8-thisphoto.jpg.

    I'd want to do this (in English-code

    Open images directory;
    if image has prefix 5 then display;
    repeat until no more found;
    close image directory;

    Thanks!
    Little Blue Plane Web Design
    Blood, Sweat & Rust - A Land Rover restoration project

  2. #2
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Location
    Pretoria, South Africa
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi
    I have had a project which needed to open a directory and list links to the files in the directory. I think that the code I used might also help you
    Code:
    <?php
    $projectContents = ''; //a clear var to add info in to be able to call it later else just echo the results later or use an array.
    $dir = "path/"; //the path to the folder containing the files
    
    // Open a known directory, and proceed to read its contents
    if (is_dir($dir)) {
        if ($dh = opendir($dir)) {
            while (($file = readdir($dh)) !== false) { //while there are files do the folowing
    			if ($file != "." && $file != ".."){ //sets a few conditions. "." and ".." is same level and up-one-level respectively 
    				$projectContents .=  '<li><a href="'.$dir.$file.'"  target="_blank">'.$file.'</a></li>'; //adds the details inta the file (or array or use echo to print the files)
    			}
            }
            closedir($dh); //the directory needs to be closed to continue
        }
    }
    ?>
    I hope that this can help you

  3. #3
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    Maybe you could use glob?

    PHP Code:
    $images glob('./path/to/images/5-*.jpg'); 
    Salathe
    Software Developer and PHP Manual Author.

  4. #4
    SitePoint Wizard bronze trophy Tailslide's Avatar
    Join Date
    Feb 2006
    Location
    Bedford, UK
    Posts
    1,687
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Never come across glob before... would that work somewhere within

    PHP Code:
    if (is_dir($dir)) { etc etc 
    More info - this would be within case studies drawn from a db. The id of the case studies is the same as the prefix for the required photos so I'd declare the $id obviously along with the path to the images and thumbs directories and then hopefully pull up the images with the prefix matching the study id. If that makes sense!
    Little Blue Plane Web Design
    Blood, Sweat & Rust - A Land Rover restoration project

  5. #5
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Location
    Pretoria, South Africa
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The way i see the glob, it does not need the dir parts. It will start on the document's level and try to find a pattern in the filesystem and then list the results that match.


    Description:
    array glob ( string $pattern [, int $flags= 0 ] )

    The glob() function searches for all the pathnames matching pattern according to the rules used by the libc glob() function, which is similar to the rules used by common shells.

    Note: This function will not work on remote files as the file to be examined must be accessible via the server's filesystem.

    Note: This function isn't available on some systems (e.g. old Sun OS).

    Note: The GLOB_BRACE flag is not available on some non GNU systems, like Solaris.

    The above is form php.net. On that page there are a lot of examples on how to use glob etc. I think that it might be even better than the way i have used and posted.

  6. #6
    SitePoint Guru
    Join Date
    Jul 2005
    Location
    Orlando
    Posts
    634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Glob is the way to go.

  7. #7
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Tailslide View Post
    Never come across glob before... would that work somewhere within

    PHP Code:
    if (is_dir($dir)) { etc etc 
    More info - this would be within case studies drawn from a db. The id of the case studies is the same as the prefix for the required photos so I'd declare the $id obviously along with the path to the images and thumbs directories and then hopefully pull up the images with the prefix matching the study id. If that makes sense!
    Sure,

    PHP Code:
    // Get directory, case id number, however you like
    $dir './blah/blah/';
    $cs_id 5;

    if (
    is_dir($dir)) {
        
    // eg. ./blah/blah/5-*.jpg
        
    $images glob($dir.$cs_id.'-*.jpg');
        
    // Use array of images... foreach, for, whatever

    Salathe
    Software Developer and PHP Manual Author.

  8. #8
    SitePoint Wizard bronze trophy Tailslide's Avatar
    Join Date
    Feb 2006
    Location
    Bedford, UK
    Posts
    1,687
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for that - I'll give it a go and report back!
    Little Blue Plane Web Design
    Blood, Sweat & Rust - A Land Rover restoration project

  9. #9
    SitePoint Wizard bronze trophy Tailslide's Avatar
    Join Date
    Feb 2006
    Location
    Bedford, UK
    Posts
    1,687
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi again.

    I'm nearly there - couple of issues.

    I've got the images displaying, but they're displaying multiple times - not sure why.

    Here's the code:

    PHP Code:
    $dir '../thumbs/';
    $photoid $id;
    $bigdir '../images/';

     foreach (
    glob($bigdir.$photoid.'-*.jpg')as $bigfilename)
     foreach (
    glob($dir.$photoid.'-*.jpg')as $filename)

       {
       echo 
    '<a href="'.$bigfilename '"><img src="'.$filename.'"/></a>';
       } 
    Thanks for your continued help!
    Little Blue Plane Web Design
    Blood, Sweat & Rust - A Land Rover restoration project

  10. #10
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    For each big image, you're looping over every single thumbnail image! Instead, you could loop over each big image but change the $filename variable to point to its associated thumbnail image.

    PHP Code:
    $dir '../thumbs/';
    $photoid $id;
    $bigdir '../images/';

    foreach (
    glob($bigdir.$photoid.'-*.jpg')as $bigfilename)
    {
        
    $filename str_replace($bigdir$dir$bigfilename);
        echo 
    '<a href="'.$bigfilename.'"><img src="'.$filename.'"/></a>';

    That of course assumes there is a thumbnail image there. Checking to see whether or not a thumbnail exists for a given big image is simple (hint: is_file($filename)).
    Salathe
    Software Developer and PHP Manual Author.

  11. #11
    SitePoint Wizard bronze trophy Tailslide's Avatar
    Join Date
    Feb 2006
    Location
    Bedford, UK
    Posts
    1,687
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Works beautifully thanks!

    Can I ask something else?

    Assuming a similar structure to the above bit of code - could you replace the echo bit with unlink() to remove all files with that prefix too?

    Thanks a million!
    Little Blue Plane Web Design
    Blood, Sweat & Rust - A Land Rover restoration project

  12. #12
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    Sure, just replace the echo line with

    PHP Code:
    unlink($bigfilename);
    unlink($filename); 
    That'll get rid of them, assuming your PHP has write access.
    Salathe
    Software Developer and PHP Manual Author.

  13. #13
    SitePoint Wizard bronze trophy Tailslide's Avatar
    Join Date
    Feb 2006
    Location
    Bedford, UK
    Posts
    1,687
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks!
    Little Blue Plane Web Design
    Blood, Sweat & Rust - A Land Rover restoration project


Bookmarks

Posting Permissions

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