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.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    May 2005
    Location
    Utah
    Posts
    65
    Thanks
    6
    Thanked 0 Times in 0 Posts

    File search - little help

    I have a list of images on my server:

    31539-ims-logo256.jpg
    32754-TU Bliss0001.jpg
    52579-beehive3.png
    46365-3D View 5.jpg
    32754-317V4FsXtcL_fix_1.png

    I want to create something to view all files with a certain number. For instance 32754 would give me 2 files in this example (there are 100's of images)

    I have:
    PHP Code:
    if(isset($_GET['searchFile'])) 

      

    and
    PHP Code:
    <a href='http://www.mysite.com/imagecheck2.php?searchFile=http://www.mysite.com/content/uploaded/' $file .   </a
    What do I need in the isset section to display the $file text images?

    Thanks for any help!

  • #2
    Regular Coder Redcoder's Avatar
    Join Date
    May 2012
    Location
    /dev/null
    Posts
    335
    Thanks
    2
    Thanked 49 Times in 48 Posts
    PHP Code:
    <?php

    if(isset($_GET['searchFile'])) 


    $search $_GET['searchFile'];
      
    $dir "/images/";

    $files scandir($dir);

    foreach(
    $files as $$key=>$value)
    {

    if(
    stripos($value$search) !== false)
    {
     
            
    $array_of_images[] = $value;


    }


    }

    }
    Then in your HTML page where you output the images, just iterate through $array_of_images

    Like this:

    PHP Code:
    foreach($array_of_images as $value)
    {

       echo 
    "<a href='".$value."'>'".$value."'</a>";  



  • Users who have thanked Redcoder for this post:

    Local Hero (12-20-2012)

  • #3
    New Coder
    Join Date
    May 2005
    Location
    Utah
    Posts
    65
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Perfect! Thanks!

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    A glob would probably be a bit easier to work with for this.
    PHP Code:
    if (isset($_GET['searchFile']) && false !== filter_var($_GET['searchFile'], FILTER_VALIDATE_INT))
    {
        
    $sSearch '/path/to/images/' . (int)$_GET['searchFile'] . '*';
        
    $aFiles glob($sSearch);
        foreach (
    $aFiles AS $file)
        {
               echo 
    "<a href='".$file."'>'".$file ."'</a>";  
        }

    Looks like it would work. I assume that searchFile will always be a number and validated it as such. Never allow a path to enter your search criteria, but if you need a string, you can always encode the / first so at least it would fail instead of granting access to higher level directories.
    There are also iterators you can work with, including the globiterator and regexiterator. The glob iterator usage is almost identical to the glob, and the regexiterator would be used in conjunction with a filesystem or directory iterator.


  •  

    Posting Permissions

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