[英]PHP displaying an image if the variable doesn't exist?
这是我一直在工作的个人图像上传网站的一些代码。 此代码的作用是根据您在地址栏中输入的内容显示图像。 现在的问题是,即使它根本不存在,它基本上也只会显示任何内容。 我想知道的是,如果要链接的图像不存在,是否仍要显示设置的图像。
<div class="container">
<div class="col-sm-12">
<div class="panel panel-default" id="welcome">
<div class="panel-heading test">
<?php
$url="".$_SERVER['REQUEST_URI'];
echo "<h2><b>$url</b>.png</h2>\n";
?>
</div>
<div class="panel-body">
<?php
$img="http://sls.pw/i".$_SERVER['REQUEST_URI'];
echo "<center><img src=\"$img.png\" class=\"img-responsive\"></center>\n";
?>
</div>
</div>
</div>
</div>
我希望我已经解释得足够好了,在此先感谢您!
你的意思是这样的吗?
<?php
//Get the Base Url
$baseUrl = $_SERVER['REQUEST_URI'];
//Get the Image Url
$imageUrl = 'http://sls.pw/i' . $baseUrl;
//Get the Headers
$headers = get_headers($imageurl);
//Check if image is availible
//$headers[0] something like this: HTTP/1.0 200 OK
$isAvailible = isset ($headers[0]) && strpos($headers[0], '200') !== false;
?>
<?php if ($isAvailible): ?>
<div class="container">
<div class="col-sm-12">
<div class="panel panel-default" id="welcome">
<div class="panel-heading test">
<h2><b><?php echo $baseUrl; ?></b>.png</h2>
</div>
<div class="panel-body" style="text-align: center;">
<img src="<?php echo $imageUrl; ?>" class="img-responsive"/>
</div>
</div>
</div>
</div>
<?php else: ?>
<p>Image not here...</p>
<?php endif; ?>
注意 :这不会保存。 考虑一下XSS等!
玩得开心 ;)
我们是在谈论同一页面而不是图像托管服务提供商吗?
然后,您应该使用file_exists()
。 如果是这样,您下次应该更加清楚。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.