在 PHP 中找不到替换图像时如何显示替换图像?

How to display a replacement image when one is not found in PHP?

我有第一个 PHP 脚本可以让我在远程服务器上显示图像。这个有效,我想设定一个条件,以便当它在 $file 变量中找不到图像时,它会在 $newfile 变量中显示图像。

但是我收到错误 "Warning: file_get_contents(): Filename cannot be empty in C:\wamp64\www..."

我的错误在哪里?

<!-- Old Script -->
<?php

$file = '//Alcyons/it/PhotoShoot/Photos_Outil/A1111_0070_1.jpg';
$type = pathinfo($file, PATHINFO_EXTENSION);

?>
</td>
<td valign=top align=center>
  <img src="<?php echo "data:image/$type;base64,",
     base64_encode(file_get_contents($file)) ?>" border=0 width="180"></a>
</td>
<td width=10></td>


<!-- New Script -->
<?php
$file = '//Alcyons/it/PhotoShoot/retail/Photos/SS20,FW1920/A1127G_00_1.jpg';
$newfile = '//Alcyons/it/PhotoShoot/retail/Photos/SS20,FW1920/A1119_4023_1.jpg';
$type = pathinfo($file, PATHINFO_EXTENSION);

?>
</td>
<td valign=top align=center>
  <img src="<?php 

  if ($file = NULL)
{

    echo "data:image/$type;base64,", base64_encode(file_get_contents($newfile));
}
else 
{  
  echo "data:image/$type;base64,", base64_encode(file_get_contents($file));

}

?>" border=0 width="180"></a>
</td>
<td width=10></td>

错误就在这里:

if ($file = NULL)

您正在将 $file 变量设置为 "null",您可能需要比较:

if ($file == NULL)

或者检查文件系统中是否存在该文件

if (!file_exists($file))