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


PHP image sorting works locally, but not online

我只是 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);
                ?>

谁能帮我?另外,我想知道是否有更简单的解决方案可以从文件夹中获取图像的名称。

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

    <?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++; 
                } 
           }

            ?> 

注意:仅提供粗略的示例;为了使其更好,请将"爆炸"作为数组的一部分进行,以节省两次。没有直接测试,因此您可能需要清理拼写错误。

相关文章: