• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PHP file_exists() function not working Codeigniter 3.1.9

#1
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.
Reply

#2
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(); 
Reply

#3
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 
}
Reply

#4
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">';

Reply

#5
Thank you very much, it worked.
Reply

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

Happy to help Cool
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.