Results 1 to 5 of 5

Thread: How to replace the deprecated eregi with preg_match in PHP Photo Album Script

  1. #1
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    619
    Thanks
    274
    Thanked 14 Times in 14 Posts

    Default How to replace the deprecated eregi with preg_match in PHP Photo Album Script

    1) Script Title: PHP Photo Album script

    2) Script URL (on DD): http://dynamicdrive.com/dynamicindex...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"

    Code:
    <?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:
    Code:
    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.
    Last edited by kuau; 09-14-2017 at 06:07 PM. Reason: a path problem made my page not work, not eregi

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,497
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    If I understand the problem correctly, I posted at least one thread on this, see:

    http://www.dynamicdrive.com/forums/s...m-script-v2-11

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

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,497
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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):

    Code:
    <?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.
    Last edited by jscheuer1; 09-15-2017 at 12:42 AM. Reason: add info
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #4
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    619
    Thanks
    274
    Thanked 14 Times in 14 Posts

    Default

    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.

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,497
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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.
    Last edited by jscheuer1; 02-25-2018 at 02:29 AM. Reason: add info
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Similar Threads

  1. "eregi" function in PHP Photo Album script v2.11 deprecated
    By Enmanuel Soria in forum Dynamic Drive scripts help
    Replies: 2
    Last Post: 01-25-2012, 04:38 PM
  2. If Function eregi() is deprecated
    By Webiter in forum PHP
    Replies: 2
    Last Post: 09-09-2011, 07:48 PM
  3. Add Photo Title to PHP Photo Album Script
    By EdgeAgain in forum Dynamic Drive scripts help
    Replies: 0
    Last Post: 09-27-2009, 11:31 PM
  4. photo album script
    By redreddy111 in forum JavaScript
    Replies: 0
    Last Post: 07-18-2009, 06:34 PM
  5. DD PHP Photo Album script - photo descrip
    By aavaughan in forum Dynamic Drive scripts help
    Replies: 4
    Last Post: 09-13-2007, 12:44 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •