PDA

View Full Version : How to replace the deprecated eregi with preg_match in PHP Photo Album Script



kuau
09-14-2017, 04:53 PM
1) Script Title: PHP Photo Album script

2) Script URL (on DD): http://dynamicdrive.com/dynamicindex4/php-photoalbum.htm

3) Describe problem: I have been using version 1 of this script for many years. It uses a little file called getpics.php which is pretty close to the getalbumpics.php of version 2. Both of them use the deprecated command "eregi"


<?php
Header("content-type: application/x-javascript");

function returnimages($dirname=".") {
$pattern="\.(jpg|jpeg|png|gif|bmp)$";
$files = array();
$curimage=0;
if($handle = opendir($dirname)) {
while(false !== ($file = readdir($handle))){
if(eregi($pattern, $file)){
$filedate=date ("M d, Y H:i:s", filemtime($file));
echo 'galleryarray[' . $curimage .']=["' . $file . '", "'.$filedate.'"];' . "\n";
$curimage++;
}
}

closedir($handle);
}
return($files);
}

echo "var galleryarray=new Array();" . "\n";
returnimages();
?>


Running under php 5.6, it works no problem, but I am trying to plan ahead because eregi will be totally removed for php 7. I have tried to use the php manual to replace eregi with preg_match but it doesn't seem to work:
if(preg_match('/$pattern/i', $file)){ I do not know JavaScript. Does Dynamic Drive have an updated version of this script for php 7? Thanks very much. I am profoundly grateful to you DD guys because, if it hadn't been for your coding assistance and DD scripts, I could never have built websites. Sincerely, thank you.

jscheuer1
09-14-2017, 11:43 PM
If I understand the problem correctly, I posted at least one thread on this, see:

http://www.dynamicdrive.com/forums/showthread.php?54416-PHP-Photo-Album-script-v2-11

Any further problems or questions, just let me know. Be as specific as possible please.

jscheuer1
09-15-2017, 12:32 AM
Ah, after closer examination, I see I might not of addressed your question. I would recommend updating and using the new getpics file from the thread I linked to in my previous post. But if you want to stick with version one and be compliant with the PHP update, it should be as simple as double delimiting the pattern (not sure why preg_match requires this, but it does, probably to allow for a practical way of including switches, which I believe the former functions didn't use) and adding the i case insensitive switch (erigi is/was case insensitive, ereg is/was not, preg_match can be case insensitive, but only with the i switch). Your version with preg_match (changed lines highlighted):


<?php
Header("content-type: application/x-javascript");

function returnimages($dirname=".") {
$pattern='/\.(jpg|jpeg|png|gif|bmp)$/i';
$files = array();
$curimage=0;
if($handle = opendir($dirname)) {
while(false !== ($file = readdir($handle))){
if(preg_match($pattern, $file)){
$filedate=date ("M d, Y H:i:s", filemtime($file));
echo 'galleryarray[' . $curimage .']=["' . $file . '", "'.$filedate.'"];' . "\n";
$curimage++;
}
}

closedir($handle);
}
return($files);
}

echo "var galleryarray=new Array();" . "\n";
returnimages();
?>

Hope that gets it for you. Let me know if there are any other issues.

I should mention that although the PHP file writes javascript. It's only the PHP code that's at issue here as far as updating to preg_match() goes.

Make sure the browser cache is cleared, and the page refreshed. Since this is a server side script, some time may also be required for possible caching of the file on the server end.

If you want more help, please post a link to the problematic page on your site so we can check it out.

kuau
02-25-2018, 01:24 AM
That fixed it! Thank you so much. I finally have a website running under php70 so was forced to update the getpics.php. I'm not entirely sure what the extra code in the getalbumpics.php does, and when I tried it, it didn't work, so is it OK if I keep using the updated getpics.php? Thanks. :)

jscheuer1
02-25-2018, 02:22 AM
Sure. It's all just PHP and javascript code. As long as it's not exposing any vulnerabilities (doesn't as far as I can tell), it's just a matter of what works.

I would add that if you're (as I understand it) using the older version of the script (the javascript part), it expects certain conventions that were in some ways redundant or at least unnecessary. Unless you update fully, you will have to use something both compatible with the version of PHP you're using and with the version of the javascript you're using. This is it.

If I'm missing any concern(s) you may still have, or if you want further clarification on anything. Just let me know.

Added later - DD took pains not to expose the PHP portion of the code to any vulnerabilities. The key part of that is it can only fetch images and only from the folder it's located in. That way it doesn't expose other folders or other file types to snooping by, well - whoever. What you see in the album is all anyone can access via the PHP part of the code, and they can see that in the album anyway.