如何使用jQuery计算目录中的文件? [英] How do I count files in a directory using jQuery?
问题描述
我想计算文件夹中的文件数量并返回该数字以设置.js文件中的变量。
I'd like to count the number of files in a folder and return that number to set a variable in a .js file.
基本上,我有X文件夹中的图像数量。当用户单击右箭头时,我将现有的8个图像集替换为下一组8个图像,依此类推。我目前手动将变量(现在38)输入到页面中,因为文件夹中有38个缩略图。
Essentially, I have X number of images in a folder. When the user clicks the right arrow, I replace the existing set of 8 images with the next set of 8 images, and so on and so forth. I currently manually enter the variable (38 right now) into the page, since there are 38 thumbnails in the folder.
代码完美无缺,但我讨厌的想法每次添加文件时更改一个数字。我想让它将变量设置为文件夹中的确切图像数。
The code works perfectly, but I hate the idea of changing a number each time I add a file. I'd like to have it set the variable to the exact number of images in the folder.
这是否可以使用JavaScript或jQuery?我是否需要编写一个PHP文件才能为我返回此变量?任何信息都会很棒!
Is this possible using JavaScript or jQuery? Do I need to write a PHP file to do return this variable for me? Any information would be great!
推荐答案
这是PHP更适合的任务,将您的Javascript保存到客户端 - 侧。编写一个非常简单的PHP脚本来计算文件夹中的文件
This is the kind of task PHP is better suited for, keep your Javascript to the client-side. Write a very simple PHP script that counts the files in a folder
echo iterator_count(new DirectoryIterator('/home/path/dir'));
并从您的JQuery脚本中调用它。
And call it from your JQuery script.
$.get('numberoffiles.php', function(data) {
alert(data);
});
这篇关于如何使用jQuery计算目录中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!