• Our team is looking to connect with folks who use email services provided by Plesk, or a premium service. If you'd like to be part of the discovery process and share your experiences, we invite you to complete this short screening survey. If your responses match the persona we are looking for, you'll receive a link to schedule a call at your convenience. We look forward to hearing from you!
  • We are looking for U.S.-based freelancer or agency working with SEO or WordPress for a quick 30-min interviews to gather feedback on XOVI, a successful German SEO tool we’re looking to launch in the U.S.
    If you qualify and participate, you’ll receive a $30 Amazon gift card as a thank-you. Please apply here. Thanks for helping shape a better SEO product for agencies!
  • The BIND DNS server has already been deprecated and removed from Plesk for Windows.
    If a Plesk for Windows server is still using BIND, the upgrade to Plesk Obsidian 18.0.70 will be unavailable until the administrator switches the DNS server to Microsoft DNS. We strongly recommend transitioning to Microsoft DNS within the next 6 weeks, before the Plesk 18.0.70 release.
  • The Horde component is removed from Plesk Installer. We recommend switching to another webmail software supported in Plesk.

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