PHP图像排序在本地工作,但不在线

时间:2012-08-16 00:46:56

标签: php html

我只是php的初学者,并且有一个PHP脚本(我从互联网上的片段放在一起),它从文件夹中获取缩略图和全尺寸图像。我希望图像按名称排序,但它们只能在我的本地MAMP服务器上进行,而不能在服务器上进行。我正在使用完全相同的php文件。

例如,在线订购3张图片(总共12张)是“查看窗口”,“40棵树”,“孤儿行走”,因为在当地排序是“40棵树”,“伊娃”,“卡罗琳“,这就是我想要的。

                <?php

                $directory = 'images/slides/other/thumbnails';
                $link = 'images/slides/other/';

                $allowed_types=array('jpg','jpeg','gif','png');
                $file_parts=array();
                $ext='';
                $title='';
                $i=0;


                $dir_handle = @opendir($directory) or die("There is an error with your image directory!");

                while ($file = readdir($dir_handle)) 
                {
                    if($file=='.' || $file == '..') continue;

                    $file_parts = explode('.',$file);
                    $ext = strtolower(array_pop($file_parts));

                    $title = implode('.',$file_parts);

                    if(in_array($ext,$allowed_types))
                    {                                           


                    // Create a new row every four columns
                    if($i % 5 == 0 and $i != 0) 
                    {
                      echo "</tr><tr>";
                    }
                    echo '<td align="middle" valign="middle"><a class="fancybox-button" rel="fancybox-button" href="'.$link.'/'.$file.'" title="'.$title.'">
                                <img src="'.$directory.'/'.$file.'"/>
                                </a>
                            </td>
                            ';

                        $i++;
                    }
                }

                closedir($dir_handle);

                ?>

任何人都可以帮助我吗?此外,我想知道是否有一个更简单的解决方案来从文件夹中获取图像的名称。

1 个答案:

答案 0 :(得分:0)

您需要将所有文件名拉入数组,然后排序。

    <?php 

            $directory = 'images/slides/other/thumbnails'; 
            $link = 'images/slides/other/'; 

            $allowed_types=array('jpg','jpeg','gif','png'); 

            $aFiles = array();


            $dir_handle = @opendir($directory) or die("There is an error with your image directory!"); 

            while ($file = readdir($dir_handle))  
            { 
                if($file=='.' || $file == '..') continue; 

                $file_parts = explode('.',$file); 
                $ext = strtolower(array_pop($file_parts)); 

                $title = implode('.',$file_parts); 

                if(in_array($ext,$allowed_types)) 
                {                                            
                    $aFiles[] = $file;
                }

            } 

            closedir($dir_handle); 

            asort($aFiles);  // Use whichever sorting function suits you best!  http://www.php.net/manual/en/array.sorting.php

            $i=0; 
            foreach ($aFiles as $file) {

                $file_parts = explode('.',$file); 
                $ext = strtolower(array_pop($file_parts)); 

                $title = implode('.',$file_parts); 


                // Create a new row every four columns 
                if($i % 5 == 0 and $i != 0)  
                { 
                  echo "</tr><tr>"; 
                } 
                echo '<td align="middle" valign="middle"><a class="fancybox-button" rel="fancybox-button" href="'.$link.'/'.$file.'" title="'.$title.'"> 
                            <img src="'.$directory.'/'.$file.'"/> 
                            </a> 
                        </td> 
                        '; 

                    $i++; 
                } 
           }


            ?> 

注意:仅粗略示例;为了使它更好,将“爆炸”作为数组的一部分来保存两次。没有直接测试,所以你可能需要清理错别字。

相关问题