duanluan8390 2013-07-02 21:30
浏览 44
已采纳

在Cakephp 2.3中显示图像

I'm coding a small news component for a website using cakephp. The ideal way in which this would work is by letting the administrator input the title for the news item, the source, the link to it (if available), its contents (to have a back up) and an accompanying image for it.

It all works but the image part. Since every entry is meant to have at most ONE image, I'm storing the image info in the same database table I'm storing the posts. I have so far managed to upload the image file to app/uploads/ and add its data (name, size and type) to the database.

Now I can't show it.

All I have managed to do looking around is set the image to be the single content of a page by using:

$this->response->file($url);

However, I need to be able to show this image as an embedded part of a website, just as most images in the web are shown. I've been looking around and can't seem to find any proper guides on doing this. The simplest solution (Linking to the image by knowing the path to the images folder) also won't work since they are in app/uploads, meaning that if I link there cakephp thinks I'm calling an 'uploads' class that doesn't exist. Using MOD_REWRITE could be an option but, in all honesty, it seems to me it would be more of a hack since there has to be a way to take a stored file and dump it as an image so that it can be embedded on the very site that's storing it.

Does anyone know about this?

  • 写回答

1条回答 默认 最新

  • dsfgdsjfd78773 2013-07-02 22:07
    关注

    For things that need to have public access for visualization, like css or js files... or in this case images, the easy option is to put it in the webroot folder.

    The usual structure I have with cake is like

    /app
       /Config
       ....
       /webroot
          /js
          /cs
          /users
             /images
             /docs
             ...etc
    

    Trying to access a folder in app (like app/uploads) will cause problems with the routing of controllers and views. So it's easier to keep things inside webroot. Specially if it's images strictly related with cake only.

    Keep in mind that things in webroot have public access, so don't keep files in there that you don't want to be accessed by url.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用