0

I've this simple PHP code in which I connect a SQLITE data base ...

<?php
   ini_set('display_errors', 1);

   echo 'Current script owner: ' . get_current_user();

   # Set access to data base...
   $db = new SQLite3('/var/www/html/OpenProntoSoccorso/Test/OpenProntoSoccorso.sqlite');

   echo ".... Connessione db OK! ...";
?>

when I try to execute from browser I've this error ...

Fatal error: Uncaught exception 'Exception' with message 'Unable to open database: unable to open database file' in /var/www/html/OpenProntoSoccorso/Test/testSpatialite.php:6 Stack trace: #0 /var/www/html/OpenProntoSoccorso/Test/testSpatialite.php(6): SQLite3->__construct('/var/www/html/O...') #1 {main} thrown in /var/www/html/OpenProntoSoccorso/Test/testSpatialite.php on line 6

but when I try to execute form command line all work fine

root@osboxes:/var/www/html/OpenProntoSoccorso/Test# ls -l
total 336536
-rwxr-x--- 1 root    root    330037248 Aug 17 14:20 OpenProntoSoccorso.sqlite
-rw-rw-r-- 1 osboxes osboxes       133 Aug 17 13:51 testSpatialite.php
root@osboxes:/var/www/html/OpenProntoSoccorso/Test# php testSpatialite.php 
.... Connessione db OK! ...root@osboxes:/var/www/html/OpenProntoSoccorso/Test#

I've tried to change owner and/or group but nothing change ...

Suggestions?

Thank you in advance!

8
  • 1
    Your permissions are wrong. Right now only root can access that database. You PHP does not (and should not) run as root on your web server. But when you are from the command line, you are logged on as root so when you execute the PHP code, it have root privileges Commented Aug 17, 2017 at 13:44
  • I've the same behavior if I try to change OpenProntoSoccorso.sqlite owner and group to "osboxes" .... Commented Aug 17, 2017 at 13:46
  • 1
    Probably because your web server does not run as osboxes but as some other unprevileged user Commented Aug 17, 2017 at 13:46
  • 1
    the quick and dirty way -> php.net/manual/en/function.get-current-user.php Commented Aug 17, 2017 at 13:48
  • 1
    Seems like it return the owner of the file. You want owner of the process. try this $processUser = posix_getpwuid(posix_geteuid()); echo $processUser['name']; (it's the first comment on the link I sent you) Commented Aug 17, 2017 at 14:18

1 Answer 1

1

Your permissions are wrong. Right now only root can access that database. You PHP does not (and should not) run as root on your web server. But when you are running it from the command line, you are logged on as root so when you execute the PHP code, it have root privileges.

To fix it, you will need to change the file permissions. for that, you can use chown. To know to which user you must transfer the ownership to, you can use this quick and dirty snippet

$processUser = posix_getpwuid(posix_geteuid());
echo $processUser['name'];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.