gpt4 book ai didi

php - 打印前检查文件是否存在

转载 作者:行者123 更新时间:2023-11-29 03:04:46 24 4
gpt4 key购买 nike

谁能帮我连接一些代码??

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

最佳答案

使用 file_exists()is_readable() 函数。

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

关于php - 打印前检查文件是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17552894/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com