michael.davis Posted September 6, 2012 Share Posted September 6, 2012 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; } ?> Quote Link to comment Share on other sites More sharing options...
premiso Posted September 6, 2012 Share Posted September 6, 2012 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). Quote Link to comment Share on other sites More sharing options...
requinix Posted September 6, 2012 Share Posted September 6, 2012 The first one is better served as a simple string comparison, though. if ($fn == "." || $fn == "..") Quote Link to comment Share on other sites More sharing options...
michael.davis Posted September 6, 2012 Author Share Posted September 6, 2012 Great Thanks! Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 6, 2012 Share Posted September 6, 2012 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; } Quote Link to comment Share on other sites More sharing options...
premiso Posted September 7, 2012 Share Posted September 7, 2012 It seems you only want files anyway, right? Shows how much I really pay attention Quote Link to comment 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.