23

I'm trying to insert data into my SQlite3 database (this works via the command line). I gave the data1.db file 777 permissions and still it says it's unable to open the file.

I get this error:

Warning: SQLite3::exec() [sqlite3.exec]: unable to open database file in /var/www/test.php on line 3

Using this code:

$db = new SQLite3('./data1.db');
$db->exec("INSERT INTO table1 (fileName) VALUES ('test.txt')");

But the strange thing is that I can read from the database: (this works)

print_r($db->querySingle('SELECT fileName FROM table1', true));

I'm running PHP 5.3.0

hakre
  • 193,403
  • 52
  • 435
  • 836
Mint
  • 14,388
  • 30
  • 76
  • 108
  • 2
    You should not need execute permission on the database - it isn't an executable script or program. You should normally avoid public write access - that means you don't care who destroys your database at any time (because anybody can do it if the file has public write permission). So, at most you should use 666, and preferably 664 or more stringent permissions. – Jonathan Leffler Sep 28 '09 at 05:47
  • 1
    Yes I know, I was just testing it to see if that was the fault or not. (luckily it wasn't). Changed it's permissions back to 644 and moved it outside the www directory. – Mint Sep 28 '09 at 05:52

3 Answers3

72

The folder containing the SQLite file must be writeable by the web user, if you want to make changes to it. It's not enough that the .db file is writeable.

See Why can't DBD::SQLite insert into a database through my Perl CGI script?

Community
  • 1
  • 1
Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
2

Try adding the sqlite user to the group which owns the /var/www directories, or manually chown -R user:user /var/www/ , the directory needs to be writable by the sqlite user.

meder omuraliev
  • 183,342
  • 71
  • 393
  • 434
  • 1
    Recursively changing ownership of the directory and all files under it would probably be a very bad idea if all that is needed is give the web server user account write access to the directory and one file in it. – user Feb 07 '11 at 13:02
0

Pay attention, if you run PHP with CLI, you must use "sudo -u www-data php script.php" so PHP can write on the sqlite.

Thomas Decaux
  • 21,738
  • 2
  • 113
  • 124