dtw52353 2014-07-16 09:01
浏览 57
已采纳

CodeIgniter file_exists无法正常工作

I saw several identical topics such as Codeigniter "file_exists($filename)", http://ellislab.com/forums/viewthread/79524/#399857., but did not worked for me.

In my URL(dashboard/products/) I call a function from the helper, and the function is the following:

if(!function_exists('getImage')){

    function getImage($image){

        $path = base_url() . 'public/images/';

        if(file_exists($path . $image) === FALSE || $image == null){
            return $path . "no_image.png";
        }

        return $path . $image;
    }
}

Then in my file content.php which is the view of dashboard/products, I use that function this way:

<img src="<?php echo getImage($row->picture); ?>" class="thumbnail-image"/>

This always returns no_image.png and the problem is caused by base_url() in the file_exists function (from what I read from other similiar topics).

The solutions told in that topics were the following:

1) - file_exists(dir_name(FCPATH)."/gallery/member_picture/member_no_image.gif")
2) - file_exists("./gallery/member_picture/member_no_image.gif")

And I've tried all of them and none worked. Just for the case, my current URL is: http://localhost/myproject/dashboard/products

Solution 1)

if(file_exists(dirname(FCPATH) . "/public/images/" . $image) === FALSE || $image == null){
    return $path . "no_image.png";
}

Does not work. If I output the values I receive the following:

return dirname(FCPATH) . "/public/images/" . $image;
//output to img src:
<img src="C:\xampp\htdocs/public/images/users/1/image_name.jpg" class="thumbnail-image">

Also, If I open the src link in another tab the URL changes to: http:// localhost/myproject/dashboard/C:/xampp/htdocs/public/images/users/1/image_name.jpg

Solution 2)

Again, adapting that to my code:

return "./public/images/" . $image;
//output to img src:
<img src="./public/images/users/1/image_name.jpg" class="thumbnail-image">

Opening the link of src in a new tab, the URL changes to:

http://localhost/myproject/dashboard/public/images/users/1/image_name.jpg

Which is wrong. It should be:

http://localhost/myproject/public/images/users/1/image_name.jpg

So, that said, how d'hell am I suppose to verify if the image exists or not?

  • 写回答

1条回答 默认 最新

  • dongyuli4538 2014-07-16 09:10
    关注

    Solved.

    Since the problem was on file_exists() and it seems that the function goes to the root directory, all I had to do was to change a little the algorithm.

    function getImage($image){
    
        if(file_exists('public/images/' . $image) == FALSE || $image == null){
            return base_url() . './public/images/no_image.png';
        }
    
        return base_url() . './public/images/' . $image;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程