user2763616
user2763616

Reputation: 21

Trying to check if file_exists in an IF statement

i am trying to check if a file exists, however it is in codeigniter behind /application/views folder.

Is there a way to check for this file OR do I need to move it outside the /application/ folder?

any help would be appreciated?

I am calling the filename from the controller like so:

    public function event() {   

    $data['main_content'] = 'event';

    $this->load->view('template', $data);

    }

then in the file 'template.php' is where I am making the IF statement for IF file_exists then display that file otherwise do nothing:

<?php

$filename = $main_content;
$exists = file_exists('application/views/scripts' . $main_content);

var_dump($exists); // just for testing

$this->load->view('templates/header');
$this->load->view($main_content);
$this->load->view('templates/footer');

if ($exists){
    $this->load->view('scripts/'.$main_content.''); 
} else{ }

?>

Upvotes: 1

Views: 3402

Answers (1)

StathisG
StathisG

Reputation: 1169

Try the following:

$filename = 'whatever';
$exists = file_exists('application/views/' . $filename);

var_dump($exists); // just for testing

Alternatively, try:

file_exists(realpath(APPPATH . '/views/' . $filename)));

I just tested both ways, both in a controller and a view, and they are working properly.

Upvotes: 2

Related Questions