cakephp 博客文章中缺少图像

发布于 2024-12-10 16:22:06 字数 1292 浏览 0 评论 0原文

我是 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'); ?>

,并且此代码显示与所有帖子相同的图像。 但我想设置特定的图像及其相关帖子......

I am new to cakePHP and I am tring the blog example of cakePHP 1.3 book .
I correctly upload image in this blog example.The image name in database and image in DOCUMENT_ROOT/....correctly
but now I am wanted to show image in my blog with related post.

I am using this code for image upload...

 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'));
                    }
    }
  }

 } 

and i am showing image form this code

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

and this show same image with all post.
but i am wanted to set specfic image with its related post....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风无影 2024-12-17 16:22:06

如果您确定一切都正确(在数据库和文件中),您应该在视图中使用类似的内容。

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

这是假设您按照教程中描述的方式将数据从控制器传递到视图视图:)

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

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

建议:使用调试工具包(cakephp 插件),这样你就可以看到传递了什么变量和结构(如 pr($variable))

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

If you are sure you are getting everything correct (in the database and the file where it should be) you should use something like this in the view.

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

this is assuming you are passing the data from the controller in the way described in the tutorial to a view view :)

if is an index view you should have a variable posts that have all post info, and in the view you will be in a loop like a foreach ($post as $post). Assuming this your view should have something like this:

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

Suggestion: use debug kit (cakephp plugin) so you can see what variables are passed down and the structure (like a pr($variable))

Hope all this helps you, if not, comment this post so i can try to extend my answer if needed

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文