打印前检查文件是否存在

时间:2013-07-09 16:00:46

标签: php mysql if-statement

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

我想要的是一个执行此操作的代码:如果存在图片然后打印它,否则不打印任何内容。 (此代码用于查找pichure) 我需要帮助才能将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>

3 个答案:

答案 0 :(得分:1)

使用file_exists()is_readable()功能。

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

答案 1 :(得分:0)

file_existsis_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.
    }
?>

顺便说一句,我建议您快速浏览所有filesystem functions,因为现在花在这上面的时间将会带来红利。

答案 2 :(得分:0)

只需使用: -

<?php
$filename = 'some_name';

if (file_exists($filename)) {
    echo "File exists";
} else {
    echo "File does not exist";
}
?>