Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-20-2012, 07:47 AM   PM User | #1
Local Hero
New Coder

 
Join Date: May 2005
Location: Utah
Posts: 58
Thanks: 6
Thanked 0 Times in 0 Posts
Local Hero is an unknown quantity at this point
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!
Local Hero is offline   Reply With Quote
Old 12-20-2012, 08:33 AM   PM User | #2
Redcoder
Regular Coder

 
Redcoder's Avatar
 
Join Date: May 2012
Location: /dev/couch
Posts: 309
Thanks: 2
Thanked 46 Times in 45 Posts
Redcoder has a little shameless behaviour in the past
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>";  


__________________
For professional Hosting and Web design.....


NetEssentials.co.uk
Redcoder is offline   Reply With Quote
Users who have thanked Redcoder for this post:
Local Hero (12-20-2012)
Old 12-20-2012, 08:44 AM   PM User | #3
Local Hero
New Coder

 
Join Date: May 2005
Location: Utah
Posts: 58
Thanks: 6
Thanked 0 Times in 0 Posts
Local Hero is an unknown quantity at this point
Perfect! Thanks!
Local Hero is offline   Reply With Quote
Old 12-20-2012, 02:31 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:43 PM.


Advertisement
Log in to turn off these ads.