使用file_exists检查echo之前是否存在图像
I'm trying to let my script check if an image exist before echoing it into my table. If it doesn't exist, it should echo another image in stead. My problem is that even though the image exist, it still goes to the else factor. Can anyone spot any coding mistakes?
<?php
mysql_select_db("xxxx");
$result = mysql_query("SELECT * FROM yyyy WHERE frivillig=1 ORDER BY stilling");
while($row = mysql_fetch_array($result))
{
?>
<tr>
<td width="50px" class="frivillig"><?php if (file_exists($url . "ansatte" . "/" . (utf8_encode($row['etternavn'])) . "%20" . (utf8_encode($row['fornavn'])) . ".jpg")) {
echo "<img" . " " . "src='" . $url . "ansatte" . "/" . (utf8_encode($row['etternavn'])) . "%20" . (utf8_encode($row['fornavn'])) . ".jpg'" . " " . "height='50px'/>";
}
else {
echo "<img" . " " . "src='" . $url . "images/mangler.png'" . " " . "height='50px'/>";
}
?>
As you can see, I use $url for my complete url, and the images are placed in the /ansatte/ folder.
Thanks for any help!
我正在尝试让我的脚本检查图像是否存在,然后将其回显到我的表中。 如果它不存在,它应该反过来回显另一个图像。 我的问题是,即使图像存在,它仍然会转向其他因素。 任何人都可以发现任何编码错误吗? p>
&lt;?php
mysql_select_db(“xxxx”);
$ result = mysql_query(“SELECT * FROM yyyy WHERE frivillig = 1 ORDER 通过静止“);
while($ row = mysql_fetch_array($ result))
{
?&gt;
&lt; tr&gt;
&lt; td width =”50px“class =”frivillig“&gt;&lt;?php if(file_exists($ url。“ansatte”。“/”。(utf8_encode($ row ['etternavn']))。“%20”。(utf8_encode($ row ['fornavn']))。“.jpg” )){
echo“&lt; img”。 “”。 “src ='”。 $ url。 “ansatte”。 “/”。 (utf8_encode($ row ['etternavn']))。 “%20”。 (utf8_encode($ row ['fornavn']))。 “.jpg'”。 “”。 “height ='50px'/&gt;”;
}
else {
echo“&lt; img”。 “”。 “src ='”。 $ url。 “images / mangler.png'”。 “”。 “height ='50px'/&gt;”;
}
?&gt;
code> pre>
如您所见,我使用$ url作为我的完整网址, 并将图像放在/ ansatte /文件夹中。 p>
感谢您的帮助! p>
div>
Consider the below snippet for checking remote resources:
$path = $url . "ansatte" . "/" . (utf8_encode($row['etternavn'])) . "%20" . (utf8_encode($row['fornavn'])) . ".jpg"
$ch = curl_init($path);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE);
$response = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpcode < 400) { // Filter out bad request, not found, etc
// found
} else {
// not accessible
}
If you don't have php5-curl installed, you could also look at the following alternatives:
getimagesize
Again only certain streams are supported here, but usage is simple and it will validate the return better:
if (getimagesize($path) !== FALSE) {
//exists
}
fopen
if (fopen($path,"r") !== FALSE) {
// exists
}
get_headers
$headers = get_headers($path, TRUE);
if ($headers !== false && isset($headers['Content-Type']) && $headers['Content-Type'] == 'image/jpeg') {
// exists
}
file_exists()
isn't intended for checking URLs. Your call to file_exists()
should reflect a relative or absolute filesystem path instead of a URL.
I'm going on a hunch here and assuming that the image file is, in fact, on the same server as the script. If so, you should be using absolute or relative paths when checking if the file exists, not a URL.
<?php
$stmt = $dbh->prepare("SELECT * FROM yyyy WHERE frivillig=1 ORDER BY stilling");
$stmt->execute();
$rows = $stmt->fetchAll();
$url = 'http://www.yoursite.com/subdir/';
$path = '/path/to/yoursite/subdir/';
foreach ($rows as $row) {
?>
<tr>
<td width="50px" class="frivillig">
<?php
if (file_exists($path . "ansatte" . "/" . (utf8_encode($row['etternavn'])) . "%20" . (utf8_encode($row['fornavn'])) . ".jpg")) {
echo "<img" . " " . "src='" . $url . "ansatte" . "/" . (utf8_encode($row['etternavn'])) . "%20" . (utf8_encode($row['fornavn'])) . ".jpg'" . " " . "height='50px'/>";
}
else {
echo "<img" . " " . "src='" . $url . "images/mangler.png'" . " " . "height='50px'/>";
}
}
?>
Also as @crush mentioned, you should not be using mysql_*
functions. They are deprecated as of PHP 5.5. The above example uses PDO.