自动创建一个json文件,其中列出了目录中的所有图像? [英] Automatically create a json file which lists all images in a directory?

查看:120
本文介绍了自动创建一个json文件,其中列出了目录中的所有图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站空间上有一个目录文件".此目录中的png图像可能会经常更改.

I have a directory "files" on my webspace. In this directory are png-images, which may change frequently.

文件名需要存储在json文件中.

The filenames need to be stored in a json file.

有没有一种方法可以自动创建一个json文件,该文件以以下方式列出目录中的所有图像:

Is there a way to automatically create a json file which lists all images in a directory in the following way:

art_showWindow({"art":[
{"href":"http://www.domain.example/files/art_noveau_1.png","title":"Art Noveau 1"},
{"href":"http://www.domain.example/files/art_noveau_5.png","title":"Art Noveau 5"},
{"href":"http://www.domain.example/files/art_noveau_23.png","title":"Art Noveau 23"},
]});

如果上面的内容太难了,标题当然可以是文件名的重复.

The title can of course just be a repetition of the filename if the above is too difficult.

这有可能吗?

预先感谢您的帮助.

托马斯

推荐答案

据我所知,没有一个神奇的原生PHP函数可以做到这一点,尽管这并不令人惊讶.

As far as I know, there is not a magical native PHP function that does exactly that, although it would not be terribly surprising.

由于为您编写脚本不会帮助您学习很多东西,所以我认为最好指出一些用于应对这一挑战的工具.

Since writing the script for you is not going to help you learn things much, I think it's best to point out a number of tools to use for this challenge.

首先,您要在目录中查找所有PNG文件;要列出目录中的所有文件(和目录),请使用 scandir($ directory) .从那里开始,您可以使用 foreach 循环遍历返回的内容数组,并且只能将PNG文件粘贴到新数组中.

First of all, you want to find all the PNG files in a directory; To list all the files (and directories) in a directory, use scandir($directory). From there on, you can use a foreach loop to iterate over the returned array, and only stick the PNG files in a new array.

由于scandir的输出无法识别文件和目录,因此您可能需要运行 is_dir ,以确保仅获取文件.

Since the output of scandir does not make files and directories discernible, you might want to run is_dir on the results to make sure you only get the files.

然后,您可以使用 pathinfo 来检索扩展名和基本名称(文件名没有扩展名,在当前文件的pathinfo输出中标记为"filename".您将需要扩展名来确定文件是否为PNG(从而将其添加到新数组中).

You can then use pathinfo to retrieve the extension and base name (filename without its extension, labeled 'filename' in the output of pathinfo) of a current file. You will need the extension to figure out whether the file is a PNG (and thus one to add to your new array).

遍历scandir数组并创建带有PNG文件名的数组后,就可以开始构建最终数组了.您将要开始另一个 foreach 循环,在该循环中添加"http://www.domain.example/files/'更改为文件名,然后做一些魔术将文件名转换为标题.您希望数组的结构如下:

Once you've iterated over the scandir array and have created an array with PNG file names, you can start building your final array. You'll want to start another foreach loop, in which you prepend 'http://www.domain.example/files/' to the file names, and do some magic to convert the file name to a title. You want to array to be structured like this:

$art = array(    
    array(
        'href' => $full_url,
        'title' => $title
    ),
    array(
        'href' => $full_url,
        'title' => $title
    ),
    // etc
);

要获得标题,我将使用这个看似复杂的单行代码:

To get the title, I'd go with this seemingly complex one-liner:

$title = ucwords( // Give words an upper case first letter
    str_replace('_', ' ', //replace underscores by spaces
        substr($filename, 0, (strlen($filename) - 4) //strip .png extension
    )
);

一旦foreach循环完成,您将拥有一个完全填充的art数组.要将其转换为JSON,只需使用 json_encode ,如下所示:

Once the foreach loop is done, you'll have a fully populate art array. To convert this to JSON, you simple use json_encode, like so:

$output = json_encode(array('art' => $art));

您提到要保存为json文件;您确定不只是希望PHP输出JSON吗?

You mention you want to save it as a json file; Are you sure you don't just want PHP to output the JSON?

无论哪种方式,保存文件都非常容易, file_put_contents 可以完成工作.

Either way, to save the file, easy enough, file_put_contents will do the job.

如果只希望PHP输出,则将Content-Type HTTP标头设置为application/json:header('Content-Type: application/json');,然后简单地回显$ output.

If you just want PHP to output it, you'll want to set the Content-Type HTTP header to application/json: header('Content-Type: application/json'); and then simply echo the $output.

希望有帮助!

(而且我意识到,这不仅需要为您编写一小段代码,而是付出了更多的努力,但是至少您仍然需要做一些思考工作才能实现它.祝您好运!

(And I realise that this took more effort than just writing you a snippet of code, but at least you'll still have to do a little think work to accomplish it. Good luck!

这篇关于自动创建一个json文件,其中列出了目录中的所有图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆