这是我的代码(在db中添加新闻)。
if(!empty($_FILES['img']['name'])){
$extension = end(explode(".",$_FILES['img']['name']));
$name = $_FILES['img']['name'];
$size = $_FILES['img']['size'];
if(file_exists("views/admin/uploads/".$name)){
$errors[] = "File with this name already exists!";
}
if($extension != "jpg" && $extension != "png" && $extension != "gif" && $extension != "JPG"){
$errors[] = "Unknown file format!";
}
}
if(count($errors)==0){
$query = mysql_query("INSERT INTO `avetisyan`.`$category` (`id`, `category`, `sub_category`, `news_category`, `title`, `img`, `short_content`, `content`, `date`, `lang`) VALUES (NULL, '$category', '$sub_category', '$news_category', '$title', '$name', '$short_content', '$long_content', '$date', '$lang')");
move_uploaded_file($_FILES['img']['tmp_name'],"views/admin/uploads/".$name);
echo "<h2 align=\"center\">Successfully added!</h2>";
}
else{
print "<h3>Errors!</h3><ul><li>".join("</li><li>",$errors)."</li></ul>";
}
如您所见,图像名称转到数据库,图像转到/ views / admin / uploads /.../ p>
在新闻中显示此图片的正确方法是什么?怎么样?
答案 0 :(得分:1)
move_uploaded_file($ _ FILES [&#39; IMG&#39;] [&#39; tmp_name的值&#39;]&#34;视图/管理/上传/&#34; $名);
你可以拥有这个&#34; views / admin / uploads&#34;在配置文件中,从配置中获取它,然后连接图像文件名&#34; $ name&#34;&#39;上传文件时
有一个单独的功能,在你的情况下提供图像路径&#34; / views / admin / uploads&#34; 从数据库中检索数据并为结果循环获取完整的图像路径
答案 1 :(得分:0)
执行查询以获取所有记录,遍历记录并执行类似此操作
<?php
$query = mysql_query("SELECT * FROM `table_name`");
if(mysql_num_rows($query) > 0)
{
for($i=0; $i<mysql_num_rows($query); $i++)
{
$row = mysql_fetch_array($query);
echo '<img src="views/admin/uploads/'.$row['img'].'" />';
}
}
?>
侧面注意:
mysql extension is deprecated as of PHP 5.5.0, and will be removed in the future.
Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an
API guide and related FAQ for more information. Alternatives to this function include:
答案 2 :(得分:0)
<?php $name = getImageNameFromDB(); ?>
<img src="<?php echo $name; ?>"/>
强制性说明: 不推荐使用mysql_ *方法,不应使用它们。此外,您的代码可能容易受到SQL Injection攻击。考虑过滤你输入甚至更好,切换到准备好的语句。
答案 3 :(得分:0)
<?php
$image_query = 'select * from `avetisyan`.`$category`';
$img = mysql_query($image_query );
while($get_items = mysql_fetch_array($img ))
{
echo '<div id="images">';
echo '<b>'.$get_items['title'].'</b>';
echo '<br><img src="uploads/'.$get_items['img'].'">';
echo '</div>';
}
?>
使用上面的代码使用适当的css样式来显示图像
答案 4 :(得分:0)
您从数据库中获取名称并将其作为标记中的源回显。例如,如果您在类别$ category中显示其图像的短篇新闻文章列表(以非常简约的布局):
$res = mysql_query("SELECT * FROM avetisyan".$category);
echo("<table>");
while ($row = mysql_fetch_assoc($res)) {
echo("<tr>");
echo("<td>");
echo("<img src=\"views/admin/uploads/".$row['img']."\">");
echo("</td>");
echo("<td>");
echo($row['short_content']);
echo("</td>");
echo("</tr>");
}
echo("</table>");