• If you are still using CentOS 7.9, it's time to convert to Alma 8 with the free centos2alma tool by Plesk or Plesk Migrator. Please let us know your experiences or concerns in this thread:
    CentOS2Alma discussion

Resolved file_put_contents() permission denied in extension

jimlongo56

Basic Pleskian
In my extension I'm trying to open and write to a file on the server.

I have the absolute path to the file ($filepath) and when I run

Code:
$contents = @file_get_contents($filepath) ?: ''; 
$s = 'some string';
$file_put_contents($contents, $s);

I get the following errors

Code:
PHP Warning: file_put_contents(/var/www/vhosts/jim.com/art.janis/.htaccess): failed to open stream: Permission denied
Line: 95
File: /usr/local/psa/admin/plib/modules/tcs-adpro/controllers/ClientController.php
Stack Trace:
#0  Smb_Exception_Syntax::handleError(2, file_put_contents(/var/www/vhosts/jim.com/art.janis/.htaccess): failed to open stream: Permission denied, /usr/local/psa/admin/plib/modules/tcs-adpro/controllers/ClientController.php, 95, Array ([domain] => art.janis.com,[webroot] => /var/www/vhosts/jim.com/art.janis,[htaccess_file] => /var/www/vhosts/jim.com/art.janis/.htaccess,[htaccess_contents] => ,[htaccess_start_comment] => # BEGIN comment, do not edit,[htaccess_end_comment] => # END comment, do not edit,[jsCode] => somerandomstring,[s] => # BEGIN comment, do not edit
AddOutputFilterByType SUBSTITUTE text/html
Substitute "somerandomstring|ni"
# END, do not edit,[php_errormsg] => file_get_contents(/var/www/vhosts/jim.com/art.janis/.htaccess): failed to open stream: No such file or directory))
#1  file_put_contents(/var/www/vhosts/jim.com/art.janis/.htaccess, # BEGIN comment, do not edit
AddOutputFilterByType SUBSTITUTE text/html
Substitute "somerandomstring|ni"
# END comment, do not edit) called at [/usr/local/psa/admin/plib/modules/tcs-adpro/controllers/ClientController.php:95]
#2  ClientController->htaccess_creator(art.janis.com, /var/www/vhosts/jim.com/art.janis) called at [/usr/local/psa/admin/plib/modules/tcs-adpro/controllers/ClientController.php:62]
#3  ClientController->indexAction() called at [/usr/local/psa/admin/plib/vendor/plesk/zendframework/library/Zend/Controller/Action.php:516]
#4  Zend_Controller_Action->dispatch(indexAction) called at [/usr/local/psa/admin/plib/vendor/plesk/zendframework/library/Zend/Controller/Dispatcher/Standard.php:308]
#5  Zend_Controller_Dispatcher_Standard->dispatch(Zend_Controller_Request_Http Object ([*_paramSources] => Array ([0] => _GET,[1] => _POST),[*_requestUri] => /modules/tcs-adpro/index.php/client/index?dom_id=28&site_id=31,[*_baseUrl] => /modules/tcs-adpro/index.php,[*_basePath] => ,[*_pathInfo] => /client/index,[*_params] => Array ([controller] => client,[action] => index,[module] => tcs-adpro),[*_rawBody] => ,[*_aliases] => Array (),[*_dispatched] => 1,[*_module] => tcs-adpro,[*_moduleKey] => module,[*_controller] => client,[*_controllerKey] => controller,[*_action] => index,[*_actionKey] => action), Zend_Controller_Response_Http Object ([*_body] => Array (),[*_exceptions] => Array (),[*_headers] => Array (),[*_headersRaw] => Array (),[*_httpResponseCode] => 200,[*_isRedirect] => ,[*_renderExceptions] => ,[headersSentThrowsException] => 1)) called at [/usr/local/psa/admin/plib/vendor/plesk/zendframework/library/Zend/Controller/Front.php:954]
#6  Zend_Controller_Front->dispatch() called at [/usr/local/psa/admin/plib/pm/Application.php:99]
#7  pm_Application->run() called at [/usr/local/psa/admin/htdocs/modules/tcs-adpro/index.php:11]

I have run
Code:
$ plesk repair installation -y -v
$ plesk repair all -y -v

I have checked that su_exec is enabled in Apache
 
Last edited:
What are the permissions of that .htaccess file?
# ll /var/www/vhosts/jim.com/art.janis/.htaccess
 
Thanks Peter for your question.

I've been advised by Support not to use the native functions, but instead to use pm_FileManager()

Code:
        $fileManager = new pm_FileManager($domainId);
        $contents = $fileManager->fileGetContents($file);
        // do something with $contents
        $fileManager->filePutContents($file, $newContent);

Extensions API Documentation - Plesk 17.5
Extensions API Documentation - Plesk 17.5

Not clear to me what fileGetContents() returns if the file is not there, may need to check if fileExists() first.
Also not clear if filePutContents() creates a file if none exists yet (native file_put_contents() does this).
 
This seems to cover the bases,

Is there a file
If not create it
Get the contents
Put the new contents


Code:
        $fileManager = new pm_FileManager($domainId);
        if(!$fileManager->fileExists($file)){
            $fileManager->touch($file);
        }
        $contents = $fileManager->fileGetContents($file);
     
        // do some manipulation of the $contents 

        $fileManager->filePutContents($file, $new_contents);
 
Back
Top