Search code examples
phpcodeigniterpathreadfile

how to read file outside codeigniter folder


i have a project to read file from outside codeigniter directory but in the same server

example :

codeigniter folder path:

opt/xampp/htdocs/yourprogramname/application

but i want to read file from :

purchase/dashboard/filename.txt

my ussual code :

  $handle = fopen("purchase/dashboard/filename.txt", "r");
  echo $handle;

  ?>

how could i do this in code igniter? i know how to read file from the same directory in codeiginiter (folder resource/etc in application) but when i tried ././ or ../ codeigniter wont read the file content


Solution

  • You can use FCPATH

    application
    purchase
    purchase > dashboard
    system
    index.php
    

    File

    <?php 
    
    if (file_exists(FCPATH . 'purchase/dashboard/filename.txt') {
    
       $handle = fopen(FCPATH . 'purchase/dashboard/filename.txt', "r");
    
       echo $handle;
    
    }
    
    ?>
    

    or

    <?php 
    
    if (file_exists('/purchase/dashboard/filename.txt') {
    
       $handle = fopen('/purchase/dashboard/filename.txt', "r");
    
       echo $handle;
    
    }
    
    ?>
    

    Codeigniter path functions definitions

    EXT: The PHP file extension
    FCPATH: Path to the front controller (this file) (root of CI)
    SELF: The name of THIS file (index.php)
    BASEPATH: Path to the system folder
    APPPATH: The path to the "application" folder