只显示了一些文件缩略图

Just some of the file thumbnails are being shown

本文关键字:文件 略图 显示      更新时间:2023-09-26

我正在将一些文件导入到一个文件夹中,使用一切正常,所有文件都能正常插入,但当我试图在浏览器上显示这些文件的缩略图时,问题就开始了。在这种情况下,只有一些文件可以完美显示,而其他文件则显示为空矩形!而所有文件都位于同一个文件夹中!我进行了更多的调查,意识到它们的原始来源(在计算机上)靠近文件夹路径的图像显示得很完美,而其他图像则没有。

例如,我的文件夹位于:c:''//examplep/htdocs/test/folder我已经把所有的图片都放在文件夹中,但只显示了最初在c:''//examplep/htdocs/test中可用的图片,而不是其他图片。

请帮我

代码:

    $images=array();  
    $dir_handler = opendir('test/folder') or die("Unable to open $path");  
    $i=0;    
    while($file = readdir($dir_handler))
    {            
    if(is_dir($file)) 
    continue;        
    else if($file != '.' && $file != '..' && $file != 'index.php')
    {                    
    $images[$i]=$file;
    $i++;     
    }       
    }      
    sort($images);
    for($i=0; $i<sizeof($images); $i++) 
    {              
    echo "<a href=".chr(34).$path.$images[$i].chr(34)."><img style='border:1px solid #666666; width:100px;height:100px; margin: 10px;' src='".$images[$i]."'/></a>";
    }        closedir($dir);

我认为您的图像src路径应该是src='folder/".$images[$i]."'

在这种情况下使用SPL的目录迭代器可能更有帮助。

在这个代码示例中,我假设您的路径是"test/folder",其中包含图像和一个index.php文件。

$path = 'test/folder';
foreach (new DirectoryIterator($path) as $fileInfo) {
    if ($fileInfo->isFile() && $fileInfo->getFilename() != 'index.php') {
        $image = "{$path}/{$fileInfo->getFilename()}";
        echo "<a href='{$image}'><img src='{$image}'></a>"; // style as appropriate
    }
}

这将在您的路径上创建一个新的DirectoryTerator。然后,如果结果确实是一个文件,而不是名为index.php的文件,那么变量$image将填充路径和文件名。echo语句有一个较短版本的示例代码,用于输出a/img标记组合。