Jump to content

PHP FOPEN User Writing


krazybob

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.