Search code examples
phpmysqlapachexampplocalhost

Localhost(XAMPP) takes too long to respond


I did not change any settings or files in xampp. Just opened xampp, start Apache and MySQL as usual. When I opened a webpage(index.php), the loading indicator in Chrome rotates counter-clockwise for 27 seconds then loads the page.

enter image description here

I only have this query in the page.

$absql = "SELECT * FROM `titles` ORDER BY date_created DESC LIMIT 90";
$result = $conn->query($absql);
// print_r($result);

if ($result->num_rows > 0) {
    // output data of each ro=w
    while ($row1 = $result->fetch_assoc()) {
        $txtID[] = $row1['uniq'];
    }
}
// print_r($txtID);

$txtsToFind = implode(",", $txtID);
$get_title_txt = glob(__DIR__ . "/library/*/*{{$txtsToFind}}.txt", GLOB_BRACE);
//Then used each file above to display details

The webpage basically loads then lists 90 images and some details.

Here's another screenshot, now it took 34 seconds before it loads. I do not know what else to add since it did not happen before. enter image description here


Solution

  • As @arkascha mentioned. glob() was the culprit for that very long server response time.

    I removed all blocks of code containing glob() and replaced it with scandir(). The response time was greatly reduced down to 236ms.

        $library = array_diff(scandir(__DIR__ . '/library'), array('..', '.'));
        $list90 = array_slice($library, 0, 90);
    
        // print_r($list90);
        foreach ($list90 as $listitem) {
            $files = preg_grep('~\.(txt)$~', scandir(__DIR__ . '/library/' . $listitem));
            $image = preg_grep('~\.(png|jpeg|jpg|webp)$~', scandir(__DIR__ . '/library/' . $listitem));
            foreach ($files as $textfile) {
                $linez = file(__DIR__ . '/library/' . $listitem.'/'.$textfile);
            }
            foreach ($image as $imahe) {
                $img = file(__DIR__ . '/library/' . $listitem.'/'.$imahe);
            }
            echo $linez[0];
            echo "<img src='library/$listitem/$imahe' alt=''>";
        }
    

    Here is the screenshot

    enter image description here