gpt4 book ai didi

mysql - cakephp 博客文章中缺少图像

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

我是 cakePHP 的新手,我正在尝试 cakePHP 1.3 书的博客示例。 我在这个博客示例中正确上传了图片。数据库中的图片名称和 DOCUMENT_ROOT/中的图片....正确但现在我想在我的博客中显示图片和相关帖子。

我正在使用此代码上传图片...

 function add() {

if (!empty($this->data)) {
if(isset($this->data["Image"]["image"]["name"])){
$file = new File($this->data["Image"]["image"]["name"]);
$ext = $file->ext();

if ($ext != 'jpg' && $ext != 'jpeg' && $ext != 'gif' && $ext != 'png') {
$this->Session->setFlash('You may only upload image files.');
}else{
if(move_uploaded_file($this->data["Image"]["image"] ["tmp_name"],$_SERVER["DOCUMENT_ROOT"]."test_om/blog/app/webroot/img/upload_image/"
. $this->data["Image"]["image"]["name"]) == true){
$this->data["Post"]["image"] = $this->data["Image"]["image"]["name"];
}

$this->Post->save($this->data);
$this->Session->setFlash('Your post has been saved.');
$this->redirect(array('action' => 'index'));
}
}
}

}

我正在显示这段代码中的图像

       <?php echo $this->Html->image('/img/upload_image/1.gif'); ?>

这显示了所有帖子的相同图像。但我想在其相关帖子中设置特定图像....

最佳答案

如果你确定你得到的一切都是正确的(在数据库和文件中它应该在的地方)你应该在 View 中使用这样的东西。

<?php echo $this->Html->image($this->data['Post']['image']); ?>

这是假设您按照教程中描述的方式将数据从 Controller 传递到 View View :)

如果是一个索引 View ,你应该有一个包含所有帖子信息的变量帖子,并且在 View 中你将像 foreach ($post as $post) 一样处于循环中。假设你的观点应该是这样的:

<?php echo $this->Html->image($post['Post']['image']); ?>

建议:使用调试工具包(cakephp 插件)这样你就可以看到哪些变量被传递下来以及结构(比如 pr($variable))

希望所有这些对您有所帮助,如果没有,请评论这篇文章,以便我可以在需要时尝试扩展我的答案

关于mysql - cakephp 博客文章中缺少图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7806860/

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