Jump to content

ereg issue


michael.davis

Recommended Posts

Hi Everyone!

 

Getting this error code with my php script that I am using.

 

Deprecated: Function ereg() is deprecated

 

Was working fine and now it is not. 

 

<?php

chdir(dirname(__FILE__));
$dir = "/sync/www/html/ohx/training";
$numberOfFiles = 6;
$pattern = '\.(pdf|xlsx|xls|doc|docx)$';

$newstamp = 0;
$newname = "";
$dc = opendir($dir);
unset($fils);
while ($fn = readdir($dc)) {
    # Eliminate current directory, parent directory
    if (ereg('^\.{1,2}$',$fn)) continue;
    # Eliminate other pages not in pattern
    if (! ereg($pattern,$fn)) continue;
    //echo "<a href='$fn'>$fn</a><br>\n";
    $timedat = filemtime("$dir/$fn");
    //$fils["a" . $timedat . "$fn"] = $fn;//keep file name and time so if 2 files have same time they dont get lost
    $fils[$fn] = $timedat;//keep file name and time so if 2 files have same time they dont get lost
    /*if ($timedat > $newstamp) {
            $newstamp = $timedat;
            $newname = $fn;
            }
            */


}

ksort ($fils, SORT_NUMERIC);
//for($i = 0; $i < $numberOfFiles ; $i++)
$fils2 = array_keys($fils);
$i = 0;
foreach($fils2 as $s){
    $i++;
echo "<a href=training/$s>$s</a><br>\n";
    //echo "$i " . $s . "<br>\n";
    if($i == $numberOfFiles )break;

}




?>

Link to comment
Share on other sites

http://php.net/ereg

 

ereg has been depreciated, you will need to convert your ereg items to preg_match

 

<?php

chdir(dirname(__FILE__));
$dir = "/sync/www/html/ohx/training";
$numberOfFiles = 6;
$pattern = '#\.(pdf|xlsx|xls|doc|docx)$#';

$newstamp = 0;
$newname = "";
$dc = opendir($dir);
unset($fils);
while ($fn = readdir($dc)) {
    # Eliminate current directory, parent directory
    if (preg_match('#^\.{1,2}$#',$fn)) continue;
    # Eliminate other pages not in pattern
    if (! preg_match($pattern,$fn)) continue;

 

Should work, I just added delimiters to the patterns, which is all it would need (the # are the delimiters).

Link to comment
Share on other sites

Hmm, I wouldn't use regular expression at all. It seems you only want files anyway, right? Are you aware that your regex (if it did work) would allow folders if they had names that ended in '.pdf' for example? I'd use glob() along with is_file() and check the extension against an allowed array.

 

[removed some lines for brevity]

$dir = "/sync/www/html/ohx/training";
$allowedTypes = array('pdf', 'xlsx', 'xls', 'doc', 'docx');

foreach(glob($dir.'/*') as $filePath)
{
    # Eliminate directories
    if(!is_file($filePath)) { continue; }

    # Eliminate disallowed filed
    $fileInfo = pathinfo($filePath);
    if(!in_array($fileInfo['extension'], $allowedTypes)) { continue; }

Link to comment
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.