简体   繁体   中英

php function file_exists with condeigniter

I am using codeigniter, I want to show a user page with his profile picture shown. When a user uploads a profile picture, it will get the same name as the username (where the old profile picture will be overwritten). If he hasn't uploaded a profile picture, then a questionmark picture is shown. I use file_exists function, but it returns every time false.

$filename = base_url().'images/userimg/'.$this->session->userdata('username');

if(file_exists($filename)){
echo '<img src="'.$filename.'" width="300" height="300"  /><br/>';
}
else { 
echo '<img src="'.base_url().'images/no_image.jpg" width="300" height="300"  /><br/>';  
}

将您的代码更改为:

$filename = './images/userimg/'.$this->session->userdata('username');

It could be that your problem results from thae usage or url instead of local server path. That base_url in the config file contains an URL and the file_exists function should work with a full local path - for which you can use FCPATH (like Svetlio suggested) or any other way. Although the file_exists should work with URL , i think there will be more trouble with it than a full path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM