使用 opendir() 按字母顺序排序
我对 PHP 很陌生,所以我仍在学习基础知识,但是我正在尝试创建一个图像库。
经过无数次 Google 搜索之后,我找到了一个 PHP 脚本,它可以完成我想要它做的事情,在查看代码并稍微操作它之后,它可以完美地与我的网站配合使用;只不过这些图像不是按字母顺序排列的。
这是我使用 scandir() 函数的代码
$max_width = 100;
$max_height = 100;
$imagedir = 'gifs/animals/'; //Remember trailing slash
function getPictureType($ext) {
if ( preg_match('/jpg|jpeg/i', $ext) ) {
return 'jpg';
} else if ( preg_match('/png/i', $ext) ) {
return 'png';
} else if ( preg_match('/gif/i', $ext) ) {
return 'gif';
} else {
return '';
}
}
function getPictures() {
global $max_width, $max_height, $imagedir;
if ( $handle = opendir($imagedir) ) {
$lightbox = rand();
echo '<ul id="pictures">';
while ( ($file = readdir($handle)) !== false ) {
if ( !is_dir($file) ) {
$split = explode($imagedir, $file);
$ext = $split[count($split) - 1];
if ( ($type = getPictureType($ext)) == '' ) {
continue;
}
$name = substr($file, 0, -4);
$title = str_replace("_"," ",$name);
echo '<li><a href="'.$name.'">';
echo '<img src="thumbs/'.$file.'" class="pictures" alt="'.$file.'" />';
echo '</a>';
echo ''.$title.'';
echo '</li>';
}
}
echo '</ul>';
}
}
,该函数可以按字母顺序对它们进行排序,但是我留下了一个数组。然后我使用 implode 函数将数组连接在一起,但之后我就陷入了困境。
任何帮助将不胜感激!
干杯。
I'm quite new to PHP so I'm still learning the very basics, however I'm trying to create an image gallery.
After countless Google searches later, I found a PHP script that does what I want it to do, and after looking at the code and manipulating it slightly it was working perfectly with my site; except that the images were not in alphabetical order.
This is the code
$max_width = 100;
$max_height = 100;
$imagedir = 'gifs/animals/'; //Remember trailing slash
function getPictureType($ext) {
if ( preg_match('/jpg|jpeg/i', $ext) ) {
return 'jpg';
} else if ( preg_match('/png/i', $ext) ) {
return 'png';
} else if ( preg_match('/gif/i', $ext) ) {
return 'gif';
} else {
return '';
}
}
function getPictures() {
global $max_width, $max_height, $imagedir;
if ( $handle = opendir($imagedir) ) {
$lightbox = rand();
echo '<ul id="pictures">';
while ( ($file = readdir($handle)) !== false ) {
if ( !is_dir($file) ) {
$split = explode($imagedir, $file);
$ext = $split[count($split) - 1];
if ( ($type = getPictureType($ext)) == '' ) {
continue;
}
$name = substr($file, 0, -4);
$title = str_replace("_"," ",$name);
echo '<li><a href="'.$name.'">';
echo '<img src="thumbs/'.$file.'" class="pictures" alt="'.$file.'" />';
echo '</a>';
echo ''.$title.'';
echo '</li>';
}
}
echo '</ul>';
}
}
I've used the scandir() function which works in sorting them alphabetically, however I was left with an array. I then used the implode function to join the array together, however after that I was stuck with what to do.
Any help would be greatly appreciated!
Cheers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
glob()
从目录中获取文件(按字母顺序排序):要迭代文件,请使用
foreach
循环:You can use
glob()
to get the files from a directory, sorted alphabetically:To iterate over your files, use a
foreach
loop:数组出了什么问题?
另外,如果您使用
pathinfo
来获取文件名和扩展名会更好。What's wrong with the arrays?
Also it would be better if you use
pathinfo
to obtain the filename and the extension.