Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php-忽略目录中缺少的文件_Php_Html - Fatal编程技术网

php-忽略目录中缺少的文件

php-忽略目录中缺少的文件,php,html,Php,Html,我用html调用一个php脚本来随机显示目录中的图像。我想通过img50.jpg将img1.jpg添加到php文件中,即使其中一些文件还不存在,这样客户机就可以随意将图像添加到目录中。 当前,如果其中一个文件不存在,图像仍会被调用并显示为丢失的文件 如何检查该文件是否存在,如果丢失则忽略它 以下是当前的php代码: <?php // rotate images randomly but w/o dups on same page - format: // <img s

我用html调用一个php脚本来随机显示目录中的图像。我想通过img50.jpg将img1.jpg添加到php文件中,即使其中一些文件还不存在,这样客户机就可以随意将图像添加到目录中。 当前,如果其中一个文件不存在,图像仍会被调用并显示为丢失的文件

如何检查该文件是否存在,如果丢失则忽略它

以下是当前的php代码:

    <?php 

// rotate images randomly but w/o dups on same page - format: 

// <img src='rotate.php?i=0'> - rotate image #0 - use 'i=1' 

// for second, etc 

// (c) 2004 David Pankhurst - use freely, but please leave in my credit 

$images=array( // list of files to rotate - add as needed 

  "img1.gif", 

  "img2.gif", 

  "img3.gif", 

  "img4.gif", 

  "img5.gif" ); 

$total=count($images); 

$secondsFixed=10; // seconds to keep list the same 

$seedValue=(int)(time()/$secondsFixed); 

srand($seedValue); 

for ($i=0;$i<$total;++$i) // shuffle list 'randomly' 

{ 

  $r=rand(0,$total-1); 

  $temp =$images[$i]; 

  $images[$i]=$images[$r]; 

  $images[$r]=$temp; 

} 

$index=(int)($_GET['i']); // image index passed in 

$i=$index%$total; // make sure index always in bounds 

$file=$images[$i]; 

header("Location: $file"); // and pass file reference back 

?>

加载所有图像后,您可以使用以下jQuery代码隐藏未加载或加载失败的图像

$("img").error(function () { 
    $(this).hide();   
});

在解析您的数组时,使用此函数检查图像是否存在

PS:您需要定义图像的路径

if (is_array(getimagesize($absolute_image)))
    return true; // image exist
else
    return false;