Jump to content

Recommended Posts

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.

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

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 />";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/242423-eregi-deprecated/#findComment-1245097
Share on other sites

@ 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

Link to comment
https://forums.phpfreaks.com/topic/242423-eregi-deprecated/#findComment-1245104
Share on other sites

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;
}

Link to comment
https://forums.phpfreaks.com/topic/242423-eregi-deprecated/#findComment-1245108
Share on other sites

@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.

Link to comment
https://forums.phpfreaks.com/topic/242423-eregi-deprecated/#findComment-1245110
Share on other sites

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'); ?>

Link to comment
https://forums.phpfreaks.com/topic/242423-eregi-deprecated/#findComment-1245117
Share on other sites


<?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'); ?>

Link to comment
https://forums.phpfreaks.com/topic/242423-eregi-deprecated/#findComment-1245120
Share on other sites

@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.

 

 

Link to comment
https://forums.phpfreaks.com/topic/242423-eregi-deprecated/#findComment-1245123
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.