2

有人可以帮我连接一些代码吗?

我想要的是执行此操作的代码:如果存在图片,则打印它,否则不打印任何东西。(此代码用于查找图片),我需要帮助将 if 语句连接到其他代码中的语句

<?php
$pathToFileOnDisk = 'inventory_images/' . $id . '.jpg';
if(file_exists($pathToFileOnDisk) && is_readable($pathToFileOnDisk)) {
<img  src="inventory_images/' . $id . '.jpg" alt="' . $name . '" width="100%" height="260" border="1" />
}
else {
// NO IMG
}
?>

<?php 
session_start();
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<?php 
// Run a select query to get my letest 6 items
// Connect to the MySQL database  
include "storescripts/connect_to_mysql.php"; 
$dynamicList = "";
$sql = mysql_query("SELECT * FROM content ORDER BY id DESC LIMIT 6");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
while($row = mysql_fetch_array($sql)){ 
         $id = $row["id"];
         $name = $row["name"];
         $content = $row["content"];
         $date_added = strftime("%d %b, %Y", strtotime($row["date_added"]));
         $dynamicList .= '<table width="100%" border="0" cellspacing="0" cellpadding="0">
         <tr>
         <td>
         <div id="left">

                <img  src="inventory_images/' . $id . '.jpg" alt="' . $name . '" width="100%" height="260" border="1" />  <-- !!THIS IS WHAT I WANNA REPLACE!!
            </div> <!-- End Left-->
    </td>
    <div id="right">


      <td width="630px" valign="top"><h2><a id="overskrift" href="product.php?id=' . $id . '">' . $name . '</a></h2>
      <p><b>
        Skrevet den ' . $date_added . '<br />
        ' . $content . '</b></p>
        </td>
        </div>
    </tr>
  </table><br />';
}
} else {
$dynamicList = "Ingen inlegg enda..";
}
mysql_close();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<link rel="stylesheet" href="style/style.css" type="text/css" media="screen" />
</head>
<body>
    <div id="wrapper">
        <header>
            <?php include_once("template_header.php");?>
        </header>  <!-- End Header -->
        <div id="banner"></div>
        <div class="content test clearfix">


            <?php echo $dynamicList; ?>
            </div> <!-- End Content -->

        <footer>
            <?php include_once("template_footer.php");?>
        </footer> <!-- End Footer -->

    </div> <!-- End Wrapper -->
</body>

4

3 回答 3

1

使用file_exists()is_readable()函数。

第一个将告诉文件是否存在,第二个将在实际执行之前告诉您是否有权读取文件。同样,还有一个is_writable()函数,它告诉您是否有权写入文件。

于 2013-07-09T16:06:09.833 回答
0

file_exists和is_readable函数非常适合此目的

例如:

<?php
    $pathToFileOnDisk = '/full/system/path/to/image.jpg';
    if(file_exists($pathToFileOnDisk) && is_readable($pathToFileOnDisk)) {
        // The file exists and can be read
    }
    else {
        // The file doesn't exist (or does exist, but PHP can't read it).
        // Perhaps output a placeholder image here.
    }
?>

顺便说一句,我建议您快速浏览一下所有文件系统功能,因为现在花在这上面的时间将在未来得到回报。

于 2013-07-09T16:02:56.417 回答
0

只需使用这个: -

<?php
$filename = 'some_name';

if (file_exists($filename)) {
    echo "File exists";
} else {
    echo "File does not exist";
}
?>
于 2013-07-09T16:05:24.553 回答