Jump to content

How to read file?


Recommended Posts

I got a problem to read data from a file. Basically, I use php function 'file_get_contents' to get data, but it turns out false.

 

The code looks like this.

$data = file_get_contents($this->module->getPath()."data/holiday.json");

echo (var_dump($data)); 

Can anybody tell me how to get the data correctly?

 

Thanks

 

 

 

Link to comment
Share on other sites

I got a problem to read data from a file. Basically, I use php function 'file_get_contents' to get data, but it turns out false.

 

The code looks like this.

$data = file_get_contents($this->module->getPath()."data/holiday.json");

echo (var_dump($data)); 

Can anybody tell me how to get the data correctly?

 

Thanks

 

Use $this->_path

 

$data = file_get_contents($this->_path . "data/holiday.json");

 

LE: After playing around with this a little bit you can also use something like this:

 

__DIR__ . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'holiday.json'

Edited by Catalin Scaesteanu (see edit history)
Link to comment
Share on other sites

@Catalin, Sorry to mention that the code is in the controller folder, but the file is in data folder.

 

$this->_path doesn't exist.

 

How to go to the parent folder using __DIR__, I tried "../" in front of it, but failed. 

Link to comment
Share on other sites

@Catalin, Sorry to mention that the code is in the controller folder, but the file is in data folder.

 

$this->_path doesn't exist.

 

How to go to the parent folder using __DIR__, I tried "../" in front of it, but failed. 

 

Ah, I see. Then you can use _PS_ROOT_DIR_ constant: file_get_contents(_PS_ROOT_DIR_ . "data/holiday.json")

  • Like 1
Link to comment
Share on other sites

×
×
  • Create New...