php
  • slider
  • nivo-slider
  • 2011-11-03 96 views 0 likes 
    0

    我想知道是否有人能够帮助我与这个PHP脚本,我正在使用JQuery Nivo幻灯片。该脚本从文本文件中获取文件名,并根据您通过分页显示的页面显示它们。PHP如果图像文件不存在,显示另一个图像

    如果文件不存在,脚本是否可以显示另一个图像?例如,如果image1.jpg不存在于images/work /目录中,那么图像不可用.jpg将会显示?

    脚本:

    <?php 
    echo" 
    
    <div class='slider-wrapper theme-custom'> 
    <div id='slider' class='nivoSlider'>"; 
    $photos=file("photos.txt"); 
    foreach($photos as $image){ 
    $item=explode("|",$image); 
    if($item[0]==$fields[0]){ 
    $photo=trim($item[1]); 
    echo"<img src='images/work/$photo' alt='' />\n"; 
    } 
    } 
    echo" 
    </div> 
    </div> 
    "?> 
    
    +0

    打印出来之前用[file_exists()](http://php.net/manual/en/function.file-exists.php)函数检查图像文件。 – xeranas

    回答

    4
    foreach ($photos as $image) { 
        ... explode 
        ... trim 
        if (!is_readable($your_document_root . '/images/work/$photo)) { 
         $photo = 'default.jpg'; // if the required photo isn't there, change to default image 
        } 
        echo blah blah blah 
    } 
    

    请注意,我用is_readable() - 有些人可能会建议使用file_exists(),但可以返回误报:该文件可能存在,但仍不能因阅读权限问题。 is_readable()将两者结合起来,并且只会在文件存在的情况下返回true。可以被网络服务器访问。

    +0

    谢谢!这是一个好方法! :) – Jess

    1

    你可以使用file_exists()如果这是你的服务器上。它将文件路径作为参数,而不是URL。

    http://php.net/manual/en/function.file-exists.php

    1

    您需要file_exists();

    返回一个布尔值。在if语句中使用它。只要你有适当的权限来访问图像。

    相关问题