Search code examples
phpapachecodeignitercodeigniter-3file-access

how to access files which is outside the var folder in linux server in code igniter 3 project


I want to access files which is outside the var folder in linux server and my codeigniter-3 project inside /var/www/html.

and the files inside /data folder.

"/data/admin_assets/upload_files/Photos/nala/start_photo.jpg" that is the path stored in the data base so when i access this url I have get 404- Not found Error


Solution

  • You can use readfile of PHP to read a file and return to browser. An example can be use like this:

    <?php
    
    $file = "/data/admin_assets/upload_files/Photos/nala/start_photo.jpg";
    
    if (file_exists($file)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename="'.basename($file).'"');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        readfile($file);
        exit;
    }
    ?>
    

    And make sure you set permission so that web server can access that directory: data/admin_assets/upload_files/Photos/nala/