× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Includes and File Paths in PHP

Geoff Douglas
Aug 21, 2014
<p><span style="font-size:18px"><strong>Q: What was the request about?&nbsp;</strong></span></p><p><span style="font-size:18px">Running a test for Facebooks PHP SDK. The programmer was getting a error like:&nbsp;</span></p><p><span style="font-size:18px">PHP Fatal error:&nbsp; require_once(): Failed opening required '/www/var/html/facebook-php-sdk-v4-master/autoload.php'<br><br><strong>Q: How did you help with the request?</strong><br>We were able to get it working, by setting up the proper include paths at the top of the script. Like so:</span></p><pre><code class="language-php">&lt;?php $basePath = dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR; set_include_path(get_include_path().PATH_SEPARATOR.$basePath.'facebook-php-sdk-v4-master'); set_include_path(get_include_path().PATH_SEPARATOR.$basePath.'facebook-php-sdk-v4-master'.DIRECTORY_SEPARATOR.'src'); ... ?&gt;</code></pre><p><span style="font-size:18px">This code grabs the absolute&nbsp;path of the script that you are currently working in, and adds other library paths to the include path.</span></p><p><br><span style="font-size:18px"><strong>Q: Any best practices &amp; key learnings you can share?</strong></span></p><p><span style="font-size:18px">Whenever you have an ad-hoc php script that you want to run/execute, you need to think about where that file is in relation to the other libraries and resources that script is going to need to run.</span></p><p><span style="font-size:18px">This is one of the major benefits to using Frameworks, such as Zend, CakePHP, Laravel, C</span>odeigniter<span style="font-size:18px">, Yii,&nbsp;etc. They set up all the high level file paths so all you need to do is focus on the code that you are writing.&nbsp;But you can't forget that all this file path stuff is going on in the background, because if something goes wrong, you will need to be able to follow the issues back to the source.</span></p>