Listing PDF files in dir with PHP 7.2

377 Views Asked by At

The function below lists all PDF files in choosen directory. Also reads and print out the song title inside the PDF (they are all songs). The script works like a charm in PHP 5.6 but not in PHP 7.2. I have to upgrade to 7.2 because of my hosting company.

function listPDF($dirtoread) {

if($handle = opendir("pdf/".$dirtoread)) {

//ny
$theFiles = array();

$count = 0;
while($currentfile = readdir($handle)) {

    if(!ereg('^\.',$currentfile)) {
        $theFiles[$count] = $currentfile;
        $count++;
    }
}

sort($theFiles,SORT_STRING);

for ($i=0; $i < count($theFiles); $i++){

        $string = file_get_contents("pdf/".$dirtoread."/".$theFiles[$i]);
        $start = strpos($string, "<dc:title>") + 10;
        $length = strpos(substr($string, $start), '</dc:title>');

        if ($length) {
            $title = strip_tags(substr($string, $start, $length));
        }


        echo "<a  class='popup gallery pdf' rel='width:700;height:600'  href='pdf/".$dirtoread."/".$theFiles[$i]."' style='text-decoration:none; line-height:2em;'><img src='pdf/pdf_small.gif' align='bottom' border='0'> ".$title."</a><br>";


}
closedir($handle);
}
}
1

There are 1 best solutions below

1
user10618987 On

Sorry! Found it. Ereg function deprecated in php 7...