How to check if view file exist in codeigniter?

php check if file exists before file_get_contents
how to get file path in codeigniter
how to open file in codeigniter
file_exists not working in codeigniter
file handling in codeigniter
php check file exists url
codeigniter file helper
php check image exists

I am working with codeigniter v3.

In the controller file i want to check if view file exist or not.

$file = "login";

$this->load->view($file, array(),true);

The code giving me error

An Error Was Encountered

Unable to load the requested file: login.php

You can use file_exists($file); but in case having absolute paths without hard coding it. you can use dirname(__FILE__)

$link = dirname(__FILE__) . '/include.php';
if (realpath($link)) {
    include($link);
}

How to check if a view exists, Get code examples like "codeigniter check view file exists" instantly right from your google search results with the Grepper Chrome Extension. I am trying to create dynamic pages without creating new files and getting the data from the database so my table will be like: Pages ----- id | page_name | text 1 | about | about page goes here 2 | contact | contact page goes here

Native PHP has the answer: file_exists() (see PHP documentation here) allows you to check whether if a file exists in your filesystem or not.

Simply put, you'd need:

if (file_exists(base_url('views/file.php')))
{
  // do something if view file exists
}

else
{
  // do some other thing if file is not present
}

codeigniter check view file exists Code Example, By default, the file does not need to exist. However, you can pass an additional argument of “true” to check that the file exists and throw FileNotFoundException() � Getting a File instance ¶ You create a new File instance by passing in the path to the file in the constructor. By default, the file does not need to exist. However, you can pass an additional argument of “true” to check that the file exists and throw FileNotFoundException() if it does not.

There is a possibility to override the function which is responsible for loading the views

create a class MY_Loader in your application/core folder.

class MY_Loader extends CI_Loader
{
    public function view($view, $vars = array(), $return = FALSE)
    {

        foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val)
        {
            $$_ci_val = isset($_ci_data[$_ci_val]) ? $_ci_data[$_ci_val] : FALSE;
        }

        $file_exists = FALSE;

        // Set the path to the requested file
        if (is_string($_ci_path) && $_ci_path !== '')
        {
            $_ci_x = explode('/', $_ci_path);
            $_ci_file = end($_ci_x);
        }
        else
        {
            $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
            $_ci_file = ($_ci_ext === '') ? $_ci_view.'.php' : $_ci_view;

            foreach ($this->_ci_view_paths as $_ci_view_file => $cascade)
            {
                if (file_exists($_ci_view_file.$_ci_file))
                {
                    $_ci_path = $_ci_view_file.$_ci_file;
                    $file_exists = TRUE;
                    break;
                }

                if ( ! $cascade)
                {
                    break;
                }
            }
        }

        if ( ! $file_exists && ! file_exists($_ci_path))
        {
            throw new Exception('View file '.$view.' doesn\'t exist.');
        }
        return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_prepare_view_vars($vars), '_ci_return' => $return));
    }

}

it throws now an exception - the only thing you have to do - is to catch it

try
{
    $file = "login";
    $this->load->view($file, array(),true); 
}
catch(\Throwable $e)
{
    echo $e->getMessage();
}

if you use PHP < 7 you need to catch an Exception instead of the Throwable Interface.

Working with Files — CodeIgniter 4.0.4 documentation, exists: <?php echo file_exists("test.txt"); The file_exists() function checks whether a file or directory exists. Specifies the path to the file or directory to check� If the file does not exist then the function will create it. Please see the PHP user NOT your controller or view files. CodeIgniter uses a front controller so

PHP file_exists() Function, provide your model code also what you are using in count_all function ? Simply what you can do you check if the value exist in database� File names are case sensitive – please check your file name. it should be in same case in view folder

[Solved] check if certain value exist in table codeigniter, file_exists — Checks whether a file or directory exists See Also � I wrote this little handy function to check if an image exists in a directory, and if so, return a� Now move all the directories and files from CodeIgniter 3.1.10 framework into codeIgniter-ajax-mysql-username-availability-check directory. We may not mention the project root directory in subsequent sections and we will assume that we are talking with respect to the project’s root directory.

file_exists - Manual, Laravel | How to check if a user is logged in, inside a blade file? Remove Extension From a Filename with PHP Example � Codeigniter 4 CRUD (� PHP’s native is_file () function is used to check whether the file is where it’s expected to be. The PageNotFoundException is a CodeIgniter exception that causes the default error page to show. In the header template, the $title variable was used to customize the page title.

Comments
  • Shouldn't one part of the code be able to expect that other parts of the code are present?
  • @JonathonReinhart most of the time I'd agree with you. However, there may be use cases in which you need to check if a case-specific (custom) view exists and display that or else display a generic one. I have a specific use case very similar to that on a site I developed for a customer. Can't go into much detail because I'm behind an NDA, but I actually needed to do exactly this at one point
  • The error is saying that not file named view.php is found in the /application/view/ folder. Checking that a "view" file exists is only a problem during development. As a developer, you are expected to supply valid values. If the user enters a non-existent resource, i.e. an non-existent page, view. whatever, CodeIgniter will display a 404 page not found view.
  • actually i am looking specifically for codeigniter framwork
  • codeigniter does not have a specific method that does that. Remember framworks are not a complete language implementation/rewrite, just a set of tools that make your life easier. Why wouldn't a native PHP function suit you?
  • My final question is basically because CI doesn't extend PHP functionality. It just wraps it. If there was (there isn't) a CI method for this, in the background it would be using the same native PHP calls. Having said that, the CI File Helper does include some file functions which, although not really aimed at checking if a file exists or not, could be used for that. Read File Helper in the CI documentation
  • yes you are right. I just wanted to know if Codeigniter have dedicated function for it or not.
  • There isn't a specific one, but the file helper provides get_file_info() and read_file(), both of which have other purposes but will return false if the file does not exist so in a way, they could be used to check if the file exists or not (they do have bigger overhead than just using file_exists() though)