Guofu Posted September 3, 2015 Share Posted September 3, 2015 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 More sharing options...
catalin.scaesteanu Posted September 3, 2015 Share Posted September 3, 2015 (edited) 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 September 4, 2015 by Catalin Scaesteanu (see edit history) Link to comment Share on other sites More sharing options...
Guofu Posted September 4, 2015 Author Share Posted September 4, 2015 @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 More sharing options...
catalin.scaesteanu Posted September 4, 2015 Share Posted September 4, 2015 @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") 1 Link to comment Share on other sites More sharing options...
Guofu Posted September 4, 2015 Author Share Posted September 4, 2015 @Catalin, thanks, do you know where are those variables defined, I may use them in the future. Link to comment Share on other sites More sharing options...
catalin.scaesteanu Posted September 4, 2015 Share Posted September 4, 2015 They're in /config/defines.inc.php 1 Link to comment Share on other sites More sharing options...
Recommended Posts