Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to read file outside codeigniter folder

Tags:

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

like image 673
Dean Huang Avatar asked Aug 22 '17 03:08

Dean Huang


2 Answers

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
like image 149
Mr. ED Avatar answered Sep 30 '22 15:09

Mr. ED


Use absolute path. Try this,

<?php 

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

?>
like image 41
Naga Avatar answered Sep 30 '22 15:09

Naga