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 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办