php/apache: Can't access file outside of root

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Paul_F
Forum Newbie
Posts: 10
Joined: Sat Aug 29, 2009 6:17 pm

php/apache: Can't access file outside of root

Post by Paul_F »

I've been trying (for 2 days) to figure out why a php script can't seem to access a folder/file outside of my root directory.

This is a fairly new install on a XP machine. Apache/2.2.11 (Win32) PHP/5.2.10.

I created a folder outside of my root called sqlite3 and granted myself 'all' privileges to it though the properties dialogue box.

To make sure (I think) that my problem is permissions, I copied the file I'm trying to access from the sqlite3 folder into my root, modified my script, and it runs.

Here's a copy of my script:

Code: Select all

 
<?php 
 
// $db = 'db.sqlite';
 
// set path of database file 
$db = $_SERVER['DOCUMENT_ROOT']."../../sqlite3/db.sqlite"; 
 
// open database file 
$handle = sqlite_open($db) or die(); 
 
// generate query string 
$query = "SELECT * FROM foo";
 
// execute query 
$result = sqlite_query($handle, $query) or die($db . ' Error in query: ' . sqlite_error_string(sqlite_last_error($handle))); 
 
// if rows exist 
if (sqlite_num_rows($result) > 0) { 
    // get each row as an array 
    // print values 
    echo '<table cellpadding=10 border=1>'; 
    while($row = sqlite_fetch_array($result)) { 
        echo '<tr>'; 
        echo '<td>'.$row[0].'</td>'; 
        echo '<td>'.$row[1].'</td>'; 
        echo '</tr>'; 
    } 
    echo '</table>'; 
} 
 
// all done 
// close database file 
sqlite_close($handle); 
?> 
 

Code: Select all

Warning: sqlite_open() [function.sqlite-open]: unable to open database: C:\sqlite3\db.sqlite in C:\public_html\snippits\sqlite.php on line 12
If I comment-out the current $db and use the $db that's currently commented, the script runs.

I think I have a permission problem on the server, but I don't understand why or how to correct it.

Can somebody help me out with this or point me in the right direction?

Thanks,
Paul
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: php/apache: Can't access file outside of root

Post by cpetercarter »

My guess is that there is an open_basedir restriction in your php.ini file which prevents scripts from opening files outside the base directory. See this.
Paul_F
Forum Newbie
Posts: 10
Joined: Sat Aug 29, 2009 6:17 pm

Re: php/apache: Can't access file outside of root

Post by Paul_F »

Thank you for the reply sir.

It turns out that I had the sqlite3 <directory C:/sqlite> setup correctly in my conf file, but, when I created the folder on my hard drive I misspelled it, slqite3.

If I didn't look at that folder name 100 times I didn't look at it once. Pretty embarassing but at least it's working now.

Paul
Post Reply