dtrt2368 2017-08-17 13:42
浏览 96
已采纳

从PHP访问SQLITE数据库时出错(但它从命令行运行...)

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!

  • 写回答

1条回答 默认 最新

  • duanjue6575 2017-08-17 14:41
    关注

    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'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 优博讯DT50高通安卓11系统刷完机自动进去fastboot模式
  • ¥15 minist数字识别
  • ¥15 在安装gym库的pygame时遇到问题,不知道如何解决
  • ¥20 uniapp中的webview 使用的是本地的vue页面,在模拟器上显示无法打开
  • ¥15 网上下载的3DMAX模型,不显示贴图怎么办
  • ¥15 关于#stm32#的问题:寻找一块开发版,作为智能化割草机的控制模块和树莓派主板相连,要求:最低可控制 3 个电机(两个驱动电机,1 个割草电机),其次可以与树莓派主板相连电机照片如下:
  • ¥15 Mac(标签-IDE|关键词-File) idea
  • ¥15 潜在扩散模型的Unet特征提取
  • ¥15 iscsi服务无法访问,如何解决?
  • ¥15 感应式传感器制作的感应式讯响器