简体   繁体   English

PHP-以特定的文件扩展名和大量记录在Json中显示内容

[英]PHP - Display Content in Json with Specific File Extension & Huge Records

As a PHP developer i am stuck. 作为一个PHP开发人员,我陷入了困境。 My mobile app developer needs an API where my PHP code will return only imagenames from a folder with following twist 我的移动应用程序开发人员需要一个API在我的PHP代码只会imagenames从文件夹返回与下面的扭曲

1) There are almost 15,000 images in wp-content/uploads/2015/04 folder, which times out the page. 1) wp-content / uploads / 2015/04文件夹中几乎有15,000张图像,导致页面超时。

What i have tried is following code which works great, if there are less number of images in that folder.What should i do in this case. 我尝试的是以下代码,如果该文件夹中的图像数量较少,那么效果很好。在这种情况下,我应该怎么办。

<?php

$dir          = "wp-content/uploads/2015/04";
$return_array = array();

if(is_dir($dir)){

    if($dh = opendir($dir)){
        while(($file = readdir($dh)) != false){

            if($file == "." or $file == ".."){

            } else {
                $return_array[] = $file; // Add the file to the array
            }
        }
    }

    echo json_encode($return_array);
}

?>

I would recommend setting the php time limit to be higher using set_time_limit(). 我建议使用set_time_limit()将php时间限制设置为更高。

Also, try caching the results in a database or file, so you don't have to do an expensive lookup every time. 另外,请尝试将结果缓存在数据库或文件中,这样就不必每次都进行昂贵的查找。

An alternative to opendir which might be faster would be to use the shell's "ls" command (note that this would be OS-specific, so reduce code portability). 可能更快的opendir替代方法是使用外壳程序的“ ls”命令(请注意,这是特定于OS的,因此降低了代码的可移植性)。

For example: 例如:

echo json_encode(explode("\n", trim(`ls -1 wp-content/uploads/2015/04`)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM