Path problems with Codeigniter on Windows

I posted how to implement Zend Framework here. However I had a problem with Window’s path when I was trying it.

I have found a couple of things about paths in Windows when I am using Codeigniter.
As all know that Windows uses \ for the path. For example, C:\xampp\htdocs\codeigniter\system\contrib. Linux uses / for the path. For example, /home/1/o/mywebsite/www/codeigniter/system/.

If you are using XAMPP or WAMP for your web development, you might have a problem with this difference.

BASEPATH in Codeigniter

Enter the following to view/welcome_message.php in your freshly installed Codeigniter.

echo 'Basepath: ' . BASEPATH . '<br />';

And visit http://127.0.0.7/codeigniter/index.php/welcome.
You will see the following.

1. Basepath: C:\xampp\htdocs\codeigniter/system/

Did you notice \codeigniter/?

In practical situation, you would add the following for your live server(Linux).

ini_set("include_path", ini_get("include_path").
PATH_SEPARATOR.BASEPATH."/contrib/");

However this will not work on your Windows.
You need to use the following instead.

ini_set("include_path", ini_get("include_path").
PATH_SEPARATOR.str_replace("/", "\\", BASEPATH)."contrib\\");

str_replace() function will replace / in BASEPATH with \.

If you want to see some of your paths in Codeigniter, add the following in your view/welcome_message.php and see the results.

<html>
<head>
<title>Welcome to CodeIgniter</title>

<style type="text/css">

body {
 background-color: #fff;
 margin: 40px;
 font-family: Lucida Grande, Verdana, Sans-serif;
 font-size: 14px;
 color: #4F5155;
}

a {
 color: #003399;
 background-color: transparent;
 font-weight: normal;
}

h1 {
 color: #444;
 background-color: transparent;
 border-bottom: 1px solid #D0D0D0;
 font-size: 16px;
 font-weight: bold;
 margin: 24px 0 2px 0;
 padding: 5px 0 6px 0;
}

code {
 font-family: Monaco, Verdana, Sans-serif;
 font-size: 12px;
 background-color: #f9f9f9;
 border: 1px solid #D0D0D0;
 color: #002166;
 display: block;
 margin: 14px 0 14px 0;
 padding: 12px 10px 12px 10px;
}

</style>
</head>
<body>
<h2>When you are using set_include_path(), before get_include_path(), it will add one semi-colon, after get_include_path.</h2>
<?php
$src_name= 'kame.png';
echo 'PART 1<br />';
>
<?=
'1. Basepath: ' . BASEPATH . '<br />'.
'2. Apppath: ' . APPPATH . '<br />'.
'3. Ext: ' . EXT . '<br />' .
'4. Fcpath: ' . FCPATH .'<br />' .
'5. Self: ' . SELF .'<br />' .
// '6. base_url(): ' . base_url() . '<br />'.
'7. site_url(): ' . site_url(). '<br />'.
'8. current_url(): ' . current_url(). '<br />'.
'9. uri_string(): ' . uri_string(). '<br />'.
'10. index_page(): ' . index_page(). '<br />'.
'11. realpath(__FILE__): ' . realpath(__FILE__). '<br />'.
'12. realpath(dirname(__FILE__)): ' . realpath(dirname(__FILE__)). '<br />'.
'13. realpath(dirname(__FILE__))/: '. realpath(dirname(__FILE__)).'/'.'<br />'.
'14. str_replace("\\", "/", realpath(dirname(__FILE__))): '. str_replace("\\", "/", realpath(dirname(__FILE__))).'<br />'.
'14a. str_replace("/", "\\", BASEPATH): '. str_replace("/", "\\", BASEPATH).'<br />'.
'15. getcwd(), current working directory: '. getcwd(). '<br />'.
'16. PATH_SEPARATOR: ' . PATH_SEPARATOR. '<br />'.
'17. basename($src_name,\'.png\')): ' . basename($src_name,'.png'). '<br />'.
'18. ini_get(\'include_path\') . PATH_SEPARATOR . APPPATH . \'libraries\': ' . ini_get('include_path') . PATH_SEPARATOR . APPPATH . 'libraries' . '<br />'.
'19. get_include_path() :'. get_include_path().PATH_SEPARATOR.'<br />' ;

>
<?php
echo '<br /><br />PART 2<br /><br />';
echo '1. dirname(dirname(__FILE__));'.dirname(dirname(__FILE__)).'<br />';
echo '2. ini_get(\'include_path\') . PATH_SEPARATOR . str_replace("/", "\\", APPPATH) . \'libraries\': ' . ini_get('include_path') . PATH_SEPARATOR . str_replace("/", "\\", APPPATH) . 'libraries' . '<br />';
echo '3. set_include_path(get_include_path() . PATH_SEPARATOR . :'. set_include_path(get_include_path() . PATH_SEPARATOR) .'<br />';
echo '4. ini_get(\'include_path\'): ' . ini_get('include_path') . '<br />';
echo '5. PATH_SEPARATOR . APPPATH . \'libraries\': ' .  PATH_SEPARATOR . APPPATH . 'libraries' . '<br />'
// 'realpath($_SERVER['DOCUMENT_ROOT']): ' . realpath($_SERVER['DOCUMENT_ROOT']).'<br />';

>

<?php
echo '<br /><br />PART 3<br /><br />';
echo '1. $_SERVER[\'DOCUMENT_ROOT\']: '. $_SERVER['DOCUMENT_ROOT'];
echo "<br />";
$base_url = '\\img\\';
$src_name = 'photo.png';
$base_path = realpath("photo.png");

echo '$base_url(): ';
echo "$base_url () <br />";
echo 'realpath(\'./img/\'): ';
echo "<br />";
echo realpath('./img/') ;
echo "<br />";
echo '$src_name: ';
echo "$src_name";
echo "<br /><br />";
echo '$_SERVER[\'DOCUMENT_ROOT\']: ';
echo  $_SERVER['DOCUMENT_ROOT'];
echo "<br />";
echo 'realpath($_SERVER[\'DOCUMENT_ROOT\']): ';
echo realpath($_SERVER['DOCUMENT_ROOT']);
echo "<br />";
$base_path = realpath('./img/');//This will show C:\xampp\htdocs\ci_day6_working_copy\img

$src_name = 'photo.png';//source name
$source_image = realpath($base_path.'/'.$src_name);
echo '$source_image is: ' . $source_image;

 echo "<br />";
$realpathtoBasepath = realpath(BASEPATH.'fonts/texb.ttf');
echo $realpathtoBasepath;
>

<?php
echo '<br /><br />PART 4<br /><br />';
echo 'Here echoing $my_include_folder = realpath(dirname(__FILE__)).\'/assets/classes\';'."<br />";
echo 'Here is the second line get_include_path() . PATH_SEPARATOR . $my_include_folder1;<br />';
$my_include_folder1 = realpath(dirname(__FILE__)).'/assets/classes1';
echo get_include_path();
echo PATH_SEPARATOR;
echo '$my_include_folder1: '.$my_include_folder1. "<br />";
echo get_include_path() . PATH_SEPARATOR . $my_include_folder1;

>

<?php
echo '<br /><br />PART 5<br /><br />';
echo 'Here echoing $my_include_folder2 = realpath(dirname(__FILE__)).\'\\assets\\classes\';'."<br />";
echo 'Here is the second line set_include_path(get_include_path() . PATH_SEPARATOR . $my_include_folder2);<br />';
$my_include_folder2 = realpath(dirname(__FILE__)).'\\assets\\classes2';
echo get_include_path() . PATH_SEPARATOR . $my_include_folder2;

>

<?php
echo '<br /><br />PART 6<br /><br />';
echo 'Here echoing ini_set("include_path", ini_get("include_path").PATH_SEPARATOR.BASEPATH."/contrib/"); '."<br />";
echo ini_get("include_path").PATH_SEPARATOR.BASEPATH."/contrib/"; 

>
<?php
echo '<br /><br />PART 7<br /><br />';
echo 'Here echoing ini_get("include_path").PATH_SEPARATOR.str_replace("/", "\\", BASEPATH)."\\contrib\\"
'."<br />";
echo ini_get("include_path").PATH_SEPARATOR.str_replace("/", "\\", BASEPATH)."contrib\\Zend\\Loader.php"

>

</body>
</html>

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>