kovhert Posted July 20, 2011 Share Posted July 20, 2011 So I just moved a client's website to a new server and now I'm getting error messages saying Deprecated: Function eregi() is deprecated in /home/artendev/public_html/gallery.php on line 27 I'm not really good with PHP, I'm more of a designer who can modify snippets and code and I didn't build this site so I need some help. Here's the original PHP: [pre]function scan_dir($dir, $regex = '\.(jpg|png|gif)$') { $objects = array_diff(scandir($dir), array('.', '..')); $object_array = array(); foreach ($objects as $object) { if (is_dir("$dir/$object")) { $i++; $object_array[$i]['title'] = $object; $object_array[$i]['images'] = scan_dir("$dir/$object"); } else if (eregi($regex, $object)) { $i++; $object_array[$i] = $object; } } return $object_array; }[/pre] What can I do to make this site work again? I tried swapping out for pregmatch (I think?) a couple of days ago, and that didn't help so I went back to eregi. The site in question: http://www.art-endeavour.co.nz/gallery.php Can someone help before I lose this client? Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/242423-eregi-deprecated/ Share on other sites More sharing options...
QuickOldCar Posted July 20, 2011 Share Posted July 20, 2011 I didn't see the reason to have the regex in the input <?php function scan_dir($dir) { $regex = "/.jpg|.png|.gif/"; $objects = array_diff(scandir($dir), array('.', '..')); $object_array = array(); foreach ($objects as $object) { if (is_dir("$dir/$object")) { $i++; $object_array[$i]['title'] = $object; $object_array[$i]['images'] = scan_dir("$dir/$object"); } else if (preg_match($regex, $object)) { $i++; $object_array[$i] = $object; } } return $object_array; } $images = scan_dir("../images"); foreach ($images as $image){ echo $image."<br />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/242423-eregi-deprecated/#findComment-1245097 Share on other sites More sharing options...
hoponhiggo Posted July 20, 2011 Share Posted July 20, 2011 Not sure if this matters, but is the new server running the latest version of PHP? Quote Link to comment https://forums.phpfreaks.com/topic/242423-eregi-deprecated/#findComment-1245100 Share on other sites More sharing options...
kovhert Posted July 20, 2011 Author Share Posted July 20, 2011 @ QuickOldCar Thanks, that got rid of the deprecated message but created more errors instead! http://www.art-endeavour.co.nz/gallerynew.php (I duplicated the page so as not to mess with the original just yet). Would you like the entire page source for the original, maybe see what's happening? @hoponhiggo Yes, I think so. Not positive though. EDIT: Just found out the servers are running PHP 5.2 Quote Link to comment https://forums.phpfreaks.com/topic/242423-eregi-deprecated/#findComment-1245104 Share on other sites More sharing options...
trq Posted July 20, 2011 Share Posted July 20, 2011 Did you replace the call to eregi() with a call to preg_match()? Also, I wouldn't remove the default regex from the argument list. Doing so has the potential to break calling code. Quote Link to comment https://forums.phpfreaks.com/topic/242423-eregi-deprecated/#findComment-1245105 Share on other sites More sharing options...
QuickOldCar Posted July 20, 2011 Share Posted July 20, 2011 edited below Quote Link to comment https://forums.phpfreaks.com/topic/242423-eregi-deprecated/#findComment-1245107 Share on other sites More sharing options...
QuickOldCar Posted July 20, 2011 Share Posted July 20, 2011 the ../images was a alocation for me to test the function function scan_dir($dir,$regex = "/.jpg|.png|.gif/" { $objects = array_diff(scandir($dir), array('.', '..')); $object_array = array(); foreach ($objects as $object) { if (is_dir("$dir/$object")) { $i++; $object_array[$i]['title'] = $object; $object_array[$i]['images'] = scan_dir("$dir/$object"); } else if (preg_match($regex, $object)) { $i++; $object_array[$i] = $object; } } return $object_array; } Quote Link to comment https://forums.phpfreaks.com/topic/242423-eregi-deprecated/#findComment-1245108 Share on other sites More sharing options...
kovhert Posted July 20, 2011 Author Share Posted July 20, 2011 @Thorpe Yeah that's what I tried at first. I don't remember the exact result, but it didn't fix the page. @QuickOldCar Yeah I noticed that and just fixed up the directory to just "images". You can see the result on the page. Apparently there's another instance of eregi on line 81 too. Here's the code for that function: <?php foreach ($dir_tree as $gallery) { if ($dir_tree[$c]['title'] === $gallery['title']) { echo '<li class="active">'; } else { echo '<li>'; } echo '<a href="' . make_url(++$i) . '">'; if ($c < 1) { if ($dh = opendir($dir . $gallery['title'])) { while (($image = readdir($dh)) !== FALSE) { if (eregi('\.(jpg|png|gif)$', $image)) { echo '<img src="resize.php?src=/' . escape_url($dir . $gallery['title'] . '/' . $image) . "&h=$cat_thumb_height" . '"><br>'; break; } } } } echo $gallery['title']; echo '</a></li>'; } ?> Okay, I'll just try that new code you gave me. Thanks. EDIT: No, that broke it. Quote Link to comment https://forums.phpfreaks.com/topic/242423-eregi-deprecated/#findComment-1245110 Share on other sites More sharing options...
kovhert Posted July 20, 2011 Author Share Posted July 20, 2011 Okay so here's the code for the original gallery page so you have a better idea of what's going on, better than I do any way <?php define('BASE_PATH', 1); include('init.php'); $template->set('page', 'gallery'); $template->add('css', array('path' => 'styles/slimbox2.css')); $template->add('js', array('path' => 'scripts/slimbox2.js')); $template->place('header'); error_reporting(E_ALL ^ E_NOTICE); //$cat_thumb_width = 310; $cat_thumb_height = 140; //$thumb_width = 140; $thumb_height = 140; function scan_dir($dir, $regex = '\.(jpg|png|gif)$') { $objects = array_diff(scandir($dir), array('.', '..')); $object_array = array(); foreach ($objects as $object) { if (is_dir("$dir/$object")) { $i++; $object_array[$i]['title'] = $object; $object_array[$i]['images'] = scan_dir("$dir/$object"); } else if (eregi($regex, $object)) { $i++; $object_array[$i] = $object; } } return $object_array; } if(!function_exists('scandir')) { function scandir($directory, $sorting_order = 0) { $dh = opendir($directory); while (($file = readdir($dh)) !== false) { $files[] = $file; } if($sorting_order == 0) { sort($files); } else { rsort($files); } return $files; } } function make_url($object) { return "gallery.php?c=$object"; } function escape_url($url) { $url = str_replace('%20', '+', $url); return str_replace('%2F', '/', urlencode($url)); } function get_request($property, $default = null) { if (isset($_REQUEST[$property])) { return $_REQUEST[$property]; } else { return $default; } } $c = get_request('c', 0); $dir = 'media/'; $dir_tree = scan_dir($dir); $dir_count = count($dir_tree); $img_count = count($dir_tree[$c]['images']); if ($c > $dir_count) { $c = 0; } ?> <h2>Gallery</h2> <ul id="gallery-categories"> <?php foreach ($dir_tree as $gallery) { if ($dir_tree[$c]['title'] === $gallery['title']) { echo '<li class="active">'; } else { echo '<li>'; } echo '<a href="' . make_url(++$i) . '">'; if ($c < 1) { if ($dh = opendir($dir . $gallery['title'])) { while (($image = readdir($dh)) !== FALSE) { if (eregi('\.(jpg|png|gif)$', $image)) { echo '<img src="resize.php?src=/' . escape_url($dir . $gallery['title'] . '/' . $image) . "&h=$cat_thumb_height" . '"><br>'; break; } } } } echo $gallery['title']; echo '</a></li>'; } ?> </ul> <?php if ($img_count > 0) { ?> <ul id="gallery-images"> <?php foreach ($dir_tree[$c]['images'] as $image) { echo '<li><a href="' . $dir . $dir_tree[$c]['title'] . '/' . $image . '" title="' . str_replace('_', ' ', substr($image, 0, strlen($image) - 4)) . '" rel="lightbox-group">'; echo '<img src="' . 'resize.php?src=/' . escape_url($dir . $dir_tree[$c]['title'] . '/' . $image) . "&h=$thumb_height" . '">'; echo '</a></li>'; } ?> </ul> <?php } ?> <?php $template->place('footer'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/242423-eregi-deprecated/#findComment-1245117 Share on other sites More sharing options...
QuickOldCar Posted July 20, 2011 Share Posted July 20, 2011 <?php define('BASE_PATH', 1); include('init.php'); $template->set('page', 'gallery'); $template->add('css', array('path' => 'styles/slimbox2.css')); $template->add('js', array('path' => 'scripts/slimbox2.js')); $template->place('header'); error_reporting(E_ALL ^ E_NOTICE); //$cat_thumb_width = 310; $cat_thumb_height = 140; //$thumb_width = 140; $thumb_height = 140; function scan_dir($dir,$regex = "/.jpg|.png|.gif/") { $objects = array_diff(scandir($dir), array('.', '..')); $object_array = array(); foreach ($objects as $object) { if (is_dir("$dir/$object")) { $i++; $object_array[$i]['title'] = $object; $object_array[$i]['images'] = scan_dir("$dir/$object"); } else if (preg_match($regex, $object)) { $i++; $object_array[$i] = $object; } } return $object_array; } if(!function_exists('scandir')) { function scandir($directory, $sorting_order = 0) { $dh = opendir($directory); while (($file = readdir($dh)) !== false) { $files[] = $file; } if($sorting_order == 0) { sort($files); } else { rsort($files); } return $files; } } function make_url($object) { return "gallery.php?c=$object"; } function escape_url($url) { $url = str_replace('%20', '+', $url); return str_replace('%2F', '/', urlencode($url)); } function get_request($property, $default = null) { if (isset($_REQUEST[$property])) { return $_REQUEST[$property]; } else { return $default; } } $c = get_request('c', 0); $dir = 'media/'; $dir_tree = scan_dir($dir); $dir_count = count($dir_tree); $img_count = count($dir_tree[$c]['images']); if ($c > $dir_count) { $c = 0; } ?> <h2>Gallery</h2> <ul id="gallery-categories"> <?php foreach ($dir_tree as $gallery) { if ($dir_tree[$c]['title'] === $gallery['title']) { echo '<li class="active">'; } else { echo '<li>'; } echo '<a href="' . make_url(++$i) . '">'; if ($c < 1) { if ($dh = opendir($dir . $gallery['title'])) { while (($image = readdir($dh)) !== FALSE) { if (preg_match("/.jpg|.png|.gif/", $image)) { echo '<img src="resize.php?src=/' . escape_url($dir . $gallery['title'] . '/' . $image) . "&h=$cat_thumb_height" . '"><br>'; break; } } } } echo $gallery['title']; echo '</a></li>'; } ?> </ul> <?php if ($img_count > 0) { ?> <ul id="gallery-images"> <?php foreach ($dir_tree[$c]['images'] as $image) { echo '<li><a href="' . $dir . $dir_tree[$c]['title'] . '/' . $image . '" title="' . str_replace('_', ' ', substr($image, 0, strlen($image) - 4)) . '" rel="lightbox-group">'; echo '<img src="' . 'resize.php?src=/' . escape_url($dir . $dir_tree[$c]['title'] . '/' . $image) . "&h=$thumb_height" . '">'; echo '</a></li>'; } ?> </ul> <?php } ?> <?php $template->place('footer'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/242423-eregi-deprecated/#findComment-1245120 Share on other sites More sharing options...
kovhert Posted July 20, 2011 Author Share Posted July 20, 2011 @QuickOldCar Awesome! You're a legend! Now I'll just swap out the original gallery page with this code and we can see if it works through all the links too. EDIT: It does! Thanks! Oh man that's a relief. Give yourself a pat on the back! Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/242423-eregi-deprecated/#findComment-1245123 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.