CodeIgniter Forums
PHP file_exists() function not working Codeigniter 3.1.9 - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: PHP file_exists() function not working Codeigniter 3.1.9 (/showthread.php?tid=71199)



PHP file_exists() function not working Codeigniter 3.1.9 - sjosephrw - 07-18-2018

I am new to this forum so please excuse me for any mistakes.
I am not sure if others are experiencing this same Issue.
I downloaded the latest version of Codeigniter (3.1.9) about a week ago. When ever I try to use the php file_exists() function to check if a image file exists it returns false even when the file exists. When ever I use !file_exists() the image file is displayed.

Thanks.


RE: PHP file_exists() function not working Codeigniter 3.1.9 - Pertti - 07-18-2018

Best print out the path you are trying to check. Usually, if full absolute path is not used, the current directory is where your index.php file is.

You can find out what your current working directory is with:
PHP Code:
echo getcwd(); 



RE: PHP file_exists() function not working Codeigniter 3.1.9 - sjosephrw - 07-18-2018

This is how I tried to print out the path I was trying to check

         //my controller
public function my_account(){
$user_id = $this->session->userdata('user_id');
               //None of these methods work
               //$file_path = getcwd().'/assets/img/user_id/'.$user_id.'.jpg';
               //$file_path = '../../assets/img/user_img/'.$user_id.'.jpg';
               //$file_path = '../assets/img/user_img/'.$user_id.'.jpg';
               //$file_path = 'assets/img/user_img/'.$user_id.'.jpg';
               //$file_path = realpath('assets/img/user_img/').'/'.$user_id.'.jpg';
               //$file_path = FCPATH.'assets/img/user_img/'.$user_id.'.jpg';
               //$file_path = $_SERVER['DOCUMENT_ROOT'].'assets/img/user_img/'.$user_id.'.jpg';
                $file_path = base_url('assets/img/user_img/').$user_id.'.jpg';

  if(file_exists($file_path)){
    $data['user_img'] = '<img src="'.$file_path.'">';;  
  } else {
  $data['user_img'] = '<img alt="No Image" src="https://dummyimage.com/100x100/000/fff">';
}


$this->load->view('templates/header');
$this->load->view('user_pages/my_account', $data);
$this->load->view('templates/footer');
//I am using Mac osx Version 10.13.5 
}


RE: PHP file_exists() function not working Codeigniter 3.1.9 - Pertti - 07-18-2018

Ah, think I see what's going on in here.

You are trying to use same path from both local and remote / web-server context.

When it's your PHP trying to access files, it's local file reference in your harddrive, but when browser tries to request a file, it'll need to add domain name, which is pointed to a web server, and web server knows which local files and folders on hard drive would be linked to said domain.

So file_exists needs local path, and img src needs remote path.

If your index.php is on same level than assets directory, this should work:
PHP Code:
$file_path 'assets/img/user_img/'.$user_id.'.jpg';
if (
file_exists($file_path)) {
 
   $data['user_img'] = '<img src="'.base_url($file_path).'">';
} else {
 
   $data['user_img'] = '<img alt="No Image" src="https://dummyimage.com/100x100/000/fff">';




RE: PHP file_exists() function not working Codeigniter 3.1.9 - sjosephrw - 07-18-2018

Thank you very much, it worked.


RE: PHP file_exists() function not working Codeigniter 3.1.9 - Pertti - 07-19-2018

(07-18-2018, 07:44 PM)sjosephrw Wrote: Thank you very much, it worked.

Happy to help Cool