I am getting this error when my php script tries to access text file.
PHP Warning: fopen(UPfileName.txt): failed to open stream: Permission denied in /path/to/script.php
Here are the permissions of my directory and file:
abc/www/file.txt
drwxr-xr-x 12 root root abc/
drwxr-xr-x 8 root root www/
-rwxrwxr-x 1 root root file.txt
Please tell what should I do to get ride of this error.
Thanks
Sam
Related
I am working on a website which needs to interact with an SQLite database. This is my first project using PHP and I'm running into some issues. So far I have:
config.php:
...
$connection = new SQLiteConnection();
$connection->connect();
And in SQLiteConnection, $connection->connect() does pretty much this:
try {
$rootDir = $_SERVER["DOCUMENT_ROOT"];
$this->pdo = new PDO("sqlite:".$rootDir.self::DATABASE_PATH);
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo "Exception: '".$e->getMessage()."' at line ".$e->getLine();
}
However each time I run config.php I get the following error:
Exception: 'could not find driver' at line 22
This is the line where I initialise a PDO.
I have tried lots of solutions:
Purging and reinstalling php-common, php-sqlite3, and others
Uncommenting these lines in my php.ini file (in /etc/php/7.2/cli):
extension=pdo_sqlite
extension=sqlite3
Uncommenting and setting an extension directory in the same php.ini file:
sqlite3.extension_dir = "ext"
Restarting the apache2 service (sudo service apache2 restart)
When I run php -m I do get the modules in the list, but with a warning:
$ php -m
PHP Warning: PHP Startup: Unable to load dynamic library 'pdo_sqlite' (tried: /usr/lib/php/20170718/pdo_sqlite (/usr/lib/php/20170718/pdo_sqlite: cannot open shared object file: No such file or directory), /usr/lib/php/20170718/pdo_sqlite.so (/usr/lib/php/20170718/pdo_sqlite.so: undefined symbol: php_pdo_unregister_driver)) in Unknown on line 0
PHP Warning: Module 'sqlite3' already loaded in Unknown on line 0
[PHP Modules]
...
PDO
pdo_sqlite
...
sqlite3
...
But when I look into the contents of the directory, those files seem to be present:
$ ls -al /usr/lib/php/20170718
total 6372
drwxr-xr-x 2 root root 4096 Mar 1 11:40 .
drwxr-xr-x 4 root root 4096 Mar 1 11:38 ..
...
-rw-r--r-- 1 root root 113048 Feb 11 15:55 pdo.so
-rw-r--r-- 1 root root 31096 Feb 11 15:55 pdo_sqlite.so
...
-rw-r--r-- 1 root root 52648 Feb 11 15:55 sqlite3.so
...
When I look into its parent directory, I see:
$ ls -al /usr/lib/php/
total 40
drwxr-xr-x 4 root root 4096 Mar 1 11:38 .
drwxr-xr-x 144 root root 4096 Mar 1 11:38 ..
drwxr-xr-x 2 root root 4096 Mar 1 11:40 20170718
drwxr-xr-x 3 root root 4096 Mar 1 11:38 7.2
-rw-r--r-- 1 root root 4845 Jan 17 2018 php-helper
-rw-r--r-- 1 root root 9534 Jan 17 2018 php-maintscript-helper
-rwxr-xr-x 1 root root 2922 Jan 17 2018 sessionclean
Could the issue be because of both 20170718 and 7.2 are present (I'm not really sure of the difference)? The 7.2 directory doesn't have any of the .so files though.
On restarting my computer (and thus the IDE I am using, IntelliJ), I received a different error when running the config.php file - that I needed to install php-cgi.
I did so and no longer received the exception when running config.php. I suppose I already solved that problem and it just required an IDE restart to apply it.
The second issue, that still remained, was the warning:
PHP Warning: PHP Startup: Unable to load dynamic library 'pdo_sqlite' (tried: /usr/lib/php/20170718/pdo_sqlite (/usr/lib/php/20170718/pdo_sqlite: cannot open shared object file: No such file or directory), /usr/lib/php/20170718/pdo_sqlite.so (/usr/lib/php/20170718/pdo_sqlite.so: undefined symbol: php_pdo_unregister_driver)) in Unknown on line 0
PHP Warning: Module 'sqlite3' already loaded in Unknown on line 0
And I resolved this using the answer here:
It means there is an extension=... or zend_extension=... line in one of your php configuration files (php.ini, or another close to it) that is trying to load that extension.
In trying to find a solution earlier, I had uncommented a few lines in the php.ini file. Simple commenting them back fixed the issue!
In case of php 7.4 on Windows, you must include the directory where libsqlite3.dll resides in the path.
See: https://www.php.net/manual/en/sqlite3.installation.php
Server: Centos 6
I have set permissions on this dev server to 0777 all the way back to the home folder (/home/...)
changed the owner to apache all all files
SELINUX is disabled
Still, when running script from browser, I get:
Warning: include_once(/home/..._somthing.php): failed to open stream:
Permission denied
I can only execute as root from the command line.
help?
I wrote a cakeshell script which I plan to use it using cronjob. while having run it manually (during testing), sometimes my site will throw SplFileInfo Warning e.g:
Warning: SplFileInfo::openFile(/var/www/flat/app/tmp/cache/persistent/myapp_cake_core_file_map):
failed to open stream: Permission denied in /var/www/flat/lib/Cake/Cache/Engine/FileEngine.php on line 313
Warning (512): SplFileInfo::openFile(/var/www/flat/app/tmp/cache/models/myapp_cake_model_default_flat_list):
failed to open stream: Permission denied [CORE/Cake/Cache/Engine/FileEngine.php, line 313]
Warning (512): SplFileInfo::openFile(/var/www/flat/app/tmp/cache/models/myapp_cake_model_default_flat_list) [http://php.net/splfileinfo.openfile]:
failed to open stream: Permission denied [CORE/Cake/Cache/Engine/FileEngine.php, line 313]
Warning (512): _cake_model_ cache was unable to write 'default_flat_list' to File cache [CORE/Cake/Cache/Cache.php, line 309]
Warning (512): SplFileInfo::openFile(/var/www/flat/app/tmp/cache/persistent/myapp_cake_core_method_cache) [http://php.net/splfileinfo.openfile]:
failed to open stream: Permission denied [CORE/Cake/Cache/Engine/FileEngine.php, line 313]
Warning (512): SplFileInfo::openFile(/var/www/flat/app/tmp/cache/persistent/myapp_cake_core_method_cache) [http://php.net/splfileinfo.openfile]:
failed to open stream: Permission denied [CORE/Cake/Cache/Engine/FileEngine.php, line 313]
Warning (512): SplFileInfo::openFile(/var/www/flat/app/tmp/cache/models/myapp_cake_model_default_flat_list) [http://php.net/splfileinfo.openfile]:
failed to open stream: Permission denied [CORE/Cake/Cache/Engine/FileEngine.php, line 313]
Warning (512): SplFileInfo::openFile(/var/www/flat/app/tmp/cache/models/myapp_cake_model_default_flat_list) [http://php.net/splfileinfo.openfile]:
failed to open stream: Permission denied [CORE/Cake/Cache/Engine/FileEngine.php, line 313]
Warning (512): _cake_model_ cache was unable to write 'default_flat_list' to File cache [CORE/Cake/Cache/Cache.php, line 309]
What happened? and how to fix this?
If I check the tmp directory on cache and persistent some of the file are under root permissions. Is this the cause?
[root#Apps103 persistent]# ls
total 40K
-rw-rw-r-- 1 apache 43 Apr 22 17:49 myapp_cake_core_cake_
-rw-rw-r-- 1 root 43 Apr 23 10:01 myapp_cake_core_cake_console_
-rw-rw-r-- 1 root 43 Apr 23 10:01 myapp_cake_core_cake_dev_
-rw-rw-r-- 1 apache 43 Apr 23 10:26 myapp_cake_core_cake_dev_en-us
-rw-rw-r-- 1 apache 43 Apr 23 10:26 myapp_cake_core_cake_en-us
-rw-rw-r-- 1 apache 43 Apr 23 10:26 myapp_cake_core_default_en-us
-rw-rw-r-- 1 root 4.3K Apr 23 10:01 myapp_cake_core_file_map
-rw-rw-r-- 1 root 4.3K Apr 23 10:01 myapp_cake_core_method_cache
I tried the solution from this link SplFileInfo::openFile(/app/tmp/cache/persistent/cake_core_cake_console_):failed to open stream:Permission denied in /lib/.../FileEngine.php line 293
I put this in bootstrap
Cache::config('default', array(
'engine' => 'File',
'mask' => 0666,
));
// short
Cache::config('short', array(
'engine' => 'File',
'duration' => '+1 hours',
'path' => CACHE,
'prefix' => 'cake_short_'
));
// long
Cache::config('long', array(
'engine' => 'File',
'duration' => '+1 week',
'probability' => 100,
'path' => CACHE . 'long' . DS,
));
but it does not work. Usually I clean up the whole tmp directory to remove the warning. then it will work fine again. I'm not sure why, but if I ran the shell, the warning error does not show, until the next day.
My tmp directory (flat/app/tmp) is under apache permission.
What happened?
The tmp folder is normally in the app dir and shared for all requests no matter the origin. A relatively common problem is for tmp files created via a web request to be read-only for console tasks and vice-versa.
If you run cli tasks as root, the tmp files will be created with the owner root - thereafter a webrequest (running by apache) may try to delete from or write to the tmp directory but be unable to modify the existing tmp folder contents as the files are owned by root.
how to fix this?
Either make cli and web requests use different tmp directories - or simply ensure that both cli and web users have write permissions to the tmp directory.
That means either defining the TMP constant before CakePHP does to a place other than app/tmp, e.g. in cake.php:
...
$ds = DIRECTORY_SEPARATOR;
define('TMP', '/tmp/myapp');
...
Or ensure the tmp folder is writable to both users, e.g.:
cd app
chmod -R 777 tmp
I have been working on a project that uses codeigninter. I worked on it in Windows 7 and it was fine but after I moved this project on Ubuntu to work on it there, It gives me this error in every page/controller/method that I try to access:
Warning: require_once(/opt/lampp/htdocs/TS_Project/system/core/CodeIgniter.php): failed to open stream: Permission denied in /opt/lampp/htdocs/TS_Project/index.php on line 202
Fatal error: require_once(): Failed opening required '/opt/lampp/htdocs/TS_Project/system/core/CodeIgniter.php' (include_path='.:/opt/lampp/lib/php') in /opt/lampp/htdocs/TS_Project/index.php on line 202
The CodeIgniter.php file does exist, the path to it is correct and I am able to read and write in it ( I can open it in a text editor and modify it freely). I tried changing the file permission to it using the chmod command but nothing happens. Why is permission denied to the CodeIgniter.php file?
You need to change the permission of your TS_Project folder to 755.
You can do that by:
sudo chmod -R 755 /opt/lampp/htdocs/TS_Project
I have a cron job that runs a php file. Within the php file it requires another file. For other php files run by cron and I do the same require file, it works just file. I've changed permissions on all files involved to 777 (for testing) and the problem still occurs. When I execute the file from the command line, it runs just fine.
Why is the file not allowed permission for this 1 php file?
Here is the error I receive from the cron:
[Fri Mar 29 00:05:01 2013] [warn-phpd] mmap cache can't open /home/rpn/public_html/admin/admin_functions.php - Permission denied (pid 29776)
PHP Warning: require(/home/rpn/public_html/admin/admin_functions.php): failed to open stream: Permission denied in /home/rp/public_html/cron/cron-boxscores.php on line 6
Warning: require(/home/rpn/public_html/admin/admin_functions.php): failed to open stream: Permission denied in /home/rp/public_html/cron/cron-boxscores.php on line 6
PHP Fatal error: require(): Failed opening required '/home/rpn/public_html/admin/admin_functions.php' (include_path='.:/usr/lib/php:/usr/local/lib/php:/home/rp/php') in /home/rp/public_html/cron/cron-scrape-boxscores.php on line 6
Fatal error: require(): Failed opening required '/home/rpn/public_html/admin/admin_functions.php' (include_path='.:/usr/lib/php:/usr/local/lib/php:/home/rp/php') in /home/rp/public_html/cron/cron-boxscores.php on line 6
Here is line 6:
require('/home/rpn/public_html/admin/admin_functions.php');
Here is the directory showing the file and permissions settings:
root#host [/home/rpn/public_html/admin]# ls -l admin*
-rw-r--r-- 1 nobody nobody 19022 Mar 8 14:18 admin_functions.php
nobody:nobody is what Apache is set to run under.