Jump to content

advice on eregi


rmagnuson

Recommended Posts

I've got a snippet of code that is giving me problems.

 

if ($handle = opendir($this->imagedir)) {
  $dest_img = $this->id . "_"; 
  while (false !== ($file = readdir($handle))) { 
    if ($file != "." && $file != ".." && eregi($dest_img,$file)) { 
      echo "<tr><td>";
      echo "$file<br><a href='index.php?mode=deletePicture&file=$file&id=$this->id'>Delete Picture</a></td>";
      echo "<td><img src='" . $this->imageurl . $file . "' border=0>";
      echo "</td></tr>";
    } 
  }
  closedir($handle); 
}

 

What's going on is that this loop looks at the directory and finds image files in it labeled XXX_X.jpg and then displays them but the problem lies in the eregi section where it will pull in pics that are irrelevant to the ID number that is passed to the loop. For instance, say you have an ID number of 22 and you pass it to the loop - it will find all the images that are like 22_1.jpg 22_2.jpg 22_3.jpg - but it will also return anything that has a larger ID with a 22 in it - so it would return pictures of 122_1.jpg 122_2.jpg and so forth. I'm thinking there has to be a better way to target the correct filenames, maybe by stripping out any extra characters from the ereg part of the loop but I'm not sure how that would be done. Any ideas?

 

Rafe

 

Link to comment
https://forums.phpfreaks.com/topic/112055-advice-on-eregi/
Share on other sites

try using glob:

 

<?php
foreach(glob("{$this->imagedir}/{$this->id}_*.jpg") as $file){
  echo "<tr><td>";
  echo "$file<br><a href='index.php?mode=deletePicture&file=$file&id=$this->id'>Delete Picture</a></td>";
  echo "<td><img src='" . $this->imageurl . $file . "' border=0>";
  echo "</td></tr>";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/112055-advice-on-eregi/#findComment-575223
Share on other sites

Thanks, that pretty much did the trick. I had to use stristr on $file though because for some reason it was returning the entire path instead of just the file so I did this on the url line:

 

echo "<td><img src='" . $this->imageurl . stristr($file, '//') . "' border=0>";

 

That obviously wouldn't work if it only had one slash though. Probably would work just as well if I had used ltrim.

 

Anyway, thanks again. This is my first time to this forum - I've been programming in php for a couple years now and never have gone to a forum for help figuring its best to hack on stuff til I find my own solutions but today my brain is fried so I thought I'd give this forum a shot. I'm glad I did!

 

Rafe

 

Link to comment
https://forums.phpfreaks.com/topic/112055-advice-on-eregi/#findComment-575255
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.