krazybob Posted May 4, 2011 Share Posted May 4, 2011 Until I switched to Plesk 9 I had no trouble writing a log file for an application that I wrote. It used PHP's FOPEN() command. It would write the file as the user. Now it does not write at all. If I run it manually it the file is owned by siteadmin but writes it as as root:root.. If I run it through a browser it is owned by apache:apache. What's changed? <?php if (file_exists('test.txt')) { unlink('test.txt'); // if it already exists let's erase it as a workaround } $fp = fopen('test.txt', 'w') or die("Unable to open file."); fwrite($fp, strftime('%c')."\r\n"); fclose($fp); ?> A similar block of code would write a ticker file that later would display on our pages. It was written and saved as the main site user, such as "siteadmin" on Plesk. Now PHP reports that it is unable to open or save the file. Permissions. We've changed nothing. This script has run since 2002 using chmod 644. We worked around this by creating an external script and using a CRON to write the file and this works fine. This suggests that the user has permission to open and write the file! Imagine our confusion. Don't laugh, but this server still has PHP 4.3.9, which adds to the confusion. It's run this way for years. PHP 4.3.9 (cgi) (built: Sep 27 2006 20:40:56) Copyright © 1997-2004 The PHP Group Zend Engine v1.3.0, Copyright © 1998-2004 Zend Technologies with the ionCube PHP Loader v3.1.16, Copyright © 2002-2006, by ionCube Ltd., and with Zend Extension Manager v1.0.10, Copyright © 2003-2006, by Zend Technologies with Zend Optimizer v3.0.1, Copyright © 1998-2006, by Zend Technologies Quote Link to comment https://forums.phpfreaks.com/topic/235535-php-fopen-user-writing/ Share on other sites More sharing options...
requinix Posted May 4, 2011 Share Posted May 4, 2011 Whatever user PHP is running as is the user that the permissions and ownership relate to. Through Apache you'll get apache:apache; through the command line you'll get whoever the user is (quite possibly root when running from cron, or whoever you are if you're running it manually). Remove the call to unlink() so the file will always be overwritten or appended to (and not recreated), then chown it to whoever you want (siteadmin?) and chmod it to 0666. Quote Link to comment https://forums.phpfreaks.com/topic/235535-php-fopen-user-writing/#findComment-1210607 Share on other sites More sharing options...
krazybob Posted May 5, 2011 Author Share Posted May 5, 2011 OK. But is it always owned by apache if a script creates it? ulink was just an experiement. But I've tried 0666 and it didn't work. Lemme try again. Quote Link to comment https://forums.phpfreaks.com/topic/235535-php-fopen-user-writing/#findComment-1210671 Share on other sites More sharing options...
requinix Posted May 5, 2011 Share Posted May 5, 2011 It's owned by whomever creates it. If PHP is running inside Apache then Apache (the user/group it's running as) owns it. Quote Link to comment https://forums.phpfreaks.com/topic/235535-php-fopen-user-writing/#findComment-1210676 Share on other sites More sharing options...
krazybob Posted May 5, 2011 Author Share Posted May 5, 2011 Often times I will download the ticker file (that's what this is) and edit it offline. Then I'll upload it. This is what changed. Previously our ticker writer did what is was supposed to and on our Plkesk server the file was owned by siteadmin and psacln. Something changed and we don't know what. Regardless of being created by a script indirectly run by another script the file was not owned by apache. But now that something has changed I can download the file but not upload it back. I tried using chown and chmod from PHP but it fails. <?php if (file_exists('test.txt')) { unlink('test.txt'); // if it already exists let's erase it as a workaround } $fp = fopen('test.txt', 'w') or die("Unable to open file."); fwrite($fp, strftime('%c')."\r\n"); fclose($fp); chown('test.txt', 'scannerbuff'); ?> or <?php if (file_exists('test.txt')) { chown('test.txt', 'siteadmin'); } $fp = fopen('test.txt', 'w') or die("Unable to open file."); fwrite($fp, strftime('%c')."\r\n"); fclose($fp); chown('test.txt', 'siteadmin'); ?> What is another way I can change ownership to siteadmin? Quote Link to comment https://forums.phpfreaks.com/topic/235535-php-fopen-user-writing/#findComment-1211067 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.