如何对PHP生成的图片列表进行排序

如何对PHP生成的图片列表进行排序,php,sorting,Php,Sorting,我使用的是JavaScriptKit.com提供的动态PHP图片查看器(v1.0),我的问题是列出的图片没有任何顺序,无论是数字、字母还是创建/修改/添加的日期。不必单独键入图像(因为有数百个图像),这很好,但由于所有图像都未排序,这很令人沮丧(因为有数百个图像)。页面上的代码本身是: <script src="../15x52_images/New_England/agetpics.php" type="text/javascript"></script> <sc

我使用的是JavaScriptKit.com提供的动态PHP图片查看器(v1.0),我的问题是列出的图片没有任何顺序,无论是数字、字母还是创建/修改/添加的日期。不必单独键入图像(因为有数百个图像),这很好,但由于所有图像都未排序,这很令人沮丧(因为有数百个图像)。页面上的代码本身是:

<script src="../15x52_images/New_England/agetpics.php" type="text/javascript"></script>
<script type="text/javascript">
    <!-- Dynamic PHP Picture Viewer (v1.0)- By JavaScriptKit.com (http://www.javascriptkit.com) //--->
    <!-- For this and over 400+ free scripts, visit JavaScript Kit- http://www.javascriptkit.com/ //--->
    <!-- This notice must stay intact for use //--->

    function populateSelect(selectobj) {
        for (var i = 0; i < picsarray.length; i++)
            selectobj.options[selectobj.options.length] = new Option(picsarray[i], picsarray[i])
        if (selectobj.options.length > 1) {
            selectobj.selectedIndex = 0
            showpicture(document.getElementById("picsform").picslist)
        }
    }

    function showpicture(selectobj) {
        piccontainerobj = document.getElementById("picturearea")
        resetfade(10)
        piccontainerobj.innerHTML = '<img src="' + locationstring + selectobj.options[selectobj.selectedIndex].value + '">'
        fadepictoview = setInterval("gradualfade(piccontainerobj)", 50)
    }

    function resetfade(degree) {
        if (window.fadepictoview)
            clearInterval(fadepictoview)
        if (typeof piccontainerobj.style.MozOpacity == "string")
            piccontainerobj.style.MozOpacity = degree / 100
        else if (piccontainerobj.filters)
            piccontainerobj.filters.alpha.opacity = degree
    }

    function gradualfade() {
        if (typeof piccontainerobj.style.MozOpacity == "string" && piccontainerobj.style.MozOpacity < 1)
            piccontainerobj.style.MozOpacity = Math.min(parseFloat(piccontainerobj.style.MozOpacity) + 0.2, 0.99)
        else if (piccontainerobj.filters && piccontainerobj.filters.alpha.opacity < 100)
            piccontainerobj.filters.alpha.opacity += 20
        else //if not IE or Moz
            clearInterval(fadepictoview)
    }

    window.onload = function() {
        populateSelect(document.getElementById("picsform").picslist)
    }
</script>

函数populateSelect(选择对象){
对于(var i=0;i1){
selectobj.selectedIndex=0
showpicture(document.getElementById(“picsform”).picslist)
}
}
功能显示图片(选择OBJ){
piccontainerobj=document.getElementById(“picturearea”)
重置淡入淡出(10)
piccontainerobj.innerHTML=''
fadepictoview=setInterval(“渐变(piccontainerobj)”,50)
}
功能重置衰减(度){
if(window.fadepictoview)
clearInterval(fadepictoview)
if(piccontainerobj.style.MozOpacity的类型==“字符串”)
piccontainerobj.style.MozOpacity=度/100
else if(piccontainerobj.filters)
piccontainerobj.filters.alpha.opacity=度数
}
函数gradualfade(){
if(typeof piccontainerobj.style.MozOpacity==“string”&&piccontainerobj.style.MozOpacity<1)
piccontainerobj.style.MozOpacity=Math.min(parseFloat(piccontainerobj.style.MozOpacity)+0.2,0.99)
else if(piccontainerobj.filters&&piccontainerobj.filters.alpha.opacity<100)
piccontainerobj.filters.alpha.opacity+=20
否则//如果不是IE或Moz
clearInterval(fadepictoview)
}
window.onload=函数(){
populateSelect(document.getElementById(“picsform”).picslist)
}
图像目录中的代码是:

<?php
Header("content-type: application/x-javascript");
$pathstring     = pathinfo($_SERVER['PHP_SELF']);
$locationstring = "http://" . $_SERVER['HTTP_HOST'] . $pathstring['dirname'] . "/";

function returnimages($dirname = ".")
{
    $pattern  = "(\.jpg$)|(\.png$)|(\.jpeg$)|(\.gif$)";
    //create an array to hold the directory list
    $files    = array();
    $curimage = 0;
    //create a handler for the directory
    if ($handle = opendir($dirname)) {
        //keep going until all files in directory are read
        while (false !== ($file = readdir($handle))) {
            if (eregi($pattern, $file)) {
                echo 'picsarray[' . $curimage . ']="' . $file . '";';
                $curimage++;
            }
        }
        //close the handler
        closedir($handle);
    }
    //return results
    return ($files);
}

echo 'var locationstring="' . $locationstring . '";';
echo 'var picsarray=new Array();';
returnimages();
?>


我列出了这两个文件,因为我不确定我必须键入哪一个请求才能进行排序,或者这是否可行。

因此,您只有一个文件名数组。例如,
$files[0]=“duol.jpg”
$files[1]=“aberforth.png”
。如果基于文件名的排序正常,只需通过
sort()
运行数组即可。否则,您将需要将数组扩展为多维数组,每个元素都是一个关联数组,或者创建一个图像对象,并将其组成一个数组。无论采用哪种方式,您都需要提取其他要排序的内容—文件扩展名、大小、修改日期等—并包含这些信息,这样您就可以构建数组,然后使用
usort()
和您自己定义的排序方法对其进行排序

或者,您可以创建自己的ImageFile类并创建这些对象的数组,而不是关联数组的数组

无论哪种方式,您都可以通过
usort()
进行排序

但是……

所有这些都是非常低效的,尤其是当你的图像数量增加时。最好将所有这些信息存储在一个数据库中——最好是在上传图像时填充。对于已经存在的图像,只需编写一个脚本来收集和插入数据一次。或者,如果您是通过scp、ftp等上传的,而不是web表单,则每隔一段时间就会运行一次cron作业,以重新扫描目录并添加或更新条目


这将减轻您在多个客户端同时尝试连接时所看到的极端负载。。。。并解决您的排序问题。您甚至可以将文件本身存储在数据库中....

对不起,这段代码在很多方面都很糟糕。。如果要对列表进行排序,可以在javascript或php中进行。
$files[0]['name']="foolish.jpg";
$files[0]['size']="1678013";
$files[0]['dlm']=filetime($files[0]['name]);