473,396 Members | 1,990 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

PHP fails writing to text file?

Hello

I'm stumped as to why PHP fails writing into a text file, with the
script being called through Apache:

==============
# pwd
/usr/local/www/data/
==============
# ll
-rwxr--r-- 1 root wheel 499 Jan 15 04:59 index.php
==============
# cat /usr/local/etc/apache/httpd.conf
User www
Group www
#BAD User root
#BAD Group wheel
==============
<?php
$fp = fopen("test.txt", "w");
fputs($fp,"First line\r\n");
fwrite($fp,"Second line\r\n");
fclose($fp);
?>
==============
http://localhost/
==============
# ll
-rwxr--r-- 1 root wheel 499 Jan 15 04:59 index.php
==============

What am I doing wrong? Is it some kind of security feature in Apache
or the OS that's preventing the script from writing the text file?

I've tried "chown www:www index.php", with no change.

Thank you.
Jan 15 '08 #1
4 2700
On Tue, 15 Jan 2008 05:12:57 +0100, Gilles Ganault <no****@nospam.com>
wrote:
>What am I doing wrong? Is it some kind of security feature in Apache
or the OS that's preventing the script from writing the text file?
Found what it was:

=======
# ll
drwxr-xrwx 2 root wheel 512 Jan 15 05:20 .

# chmod 757 ./.
=======

Now, index.php can write into Apache's htdocs/ but I doubt this is the
right solution. Does it mean that PHP scripts shouldn't write any file
into htdocs/ ?

Thanks.
Jan 15 '08 #2
Gilles Ganault wrote:
On Tue, 15 Jan 2008 05:12:57 +0100, Gilles Ganault <no****@nospam.com>
wrote:
>What am I doing wrong? Is it some kind of security feature in Apache
or the OS that's preventing the script from writing the text file?

Found what it was:

=======
# ll
drwxr-xrwx 2 root wheel 512 Jan 15 05:20 .

# chmod 757 ./.
=======

Now, index.php can write into Apache's htdocs/ but I doubt this is the
right solution. Does it mean that PHP scripts shouldn't write any file
into htdocs/ ?

Thanks.
Well to your problem yes it is the solution. Your running PHP as an
apache module, which makes the script run as www.

that htdocs folder is not owned by www nor in same group, so you have to
have 'Other' Permissions with Write access (the 3rd digit thats a 7 =
all access for Other).

Now, if your in a dedicated hosting environment, this is fine, but can
be annoying when writing files.

If you plan on hosting anyone elses site, or if you just want a little
more security/ease of file permissions look into suPHP.

suPHP is nice because it allows the php process to run as the same group
as the user running it, in other words does not require anything but
Owner permissions for Writing.

You could also chown the directory your writing too also to allow
access, or touch test.txt, chown www:www test.txt , and chmod 0755 test.txt

That way the file is already created, and index.php has permission to
write to it.
--
Daniel Ennis
faNetworks.net - Quality Web Hosting and Ventrilo Services
System Administrator / Web Developer
PHP Developer for 6 years
da****@fanetworks.net
Jan 15 '08 #3
Gilles Ganault wrote:
On Tue, 15 Jan 2008 05:12:57 +0100, Gilles Ganault <no****@nospam.com>
wrote:
>What am I doing wrong? Is it some kind of security feature in Apache
or the OS that's preventing the script from writing the text file?

Found what it was:

=======
# ll
drwxr-xrwx 2 root wheel 512 Jan 15 05:20 .

# chmod 757 ./.
=======

Now, index.php can write into Apache's htdocs/ but I doubt this is the
right solution. Does it mean that PHP scripts shouldn't write any file
into htdocs/ ?

Thanks.
Well, it's not necessarily a good idea - it's too easy to upload
malicious scripts. But with proper controls, it is possible.

chmod 757 will work, but as you found, is dangerous. Safer would be to
make the Apache user the owner of the directory, or at least a member of
the group which owns the directory.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Jan 15 '08 #4
On Tue, 15 Jan 2008 06:42:07 -0500, Jerry Stuckle
<js*******@attglobal.netwrote:
>chmod 757 will work, but as you found, is dangerous. Safer would be to
make the Apache user the owner of the directory, or at least a member of
the group which owns the directory.
Right, I'd rather do this:

cd /usr/local/www/data ; chown www:www ./.

Thanks also Daniel for suPHP. I'll take a look.
Jan 16 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Jack | last post by:
I'm trying to make a script that will search a list of files and remove the following line <meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>"> I'm doing this...
7
by: Randell D. | last post by:
Whats wrong with my sample script below? All I want to do is confirm that my form data is in my environment (basically, I'm writing a script that will check that some form fields have a value, and...
25
by: H.A. Sujith | last post by:
If malloc fails what should I do? 1. Exit imediately. 2. Print an error message (or put a log entry) and exit. 3. Print an error message (or put a log entry) and continue execution (after...
3
by: Steve Yerkes | last post by:
There seems to be way too much confusion over how to set focus on the a field using a field validator. I looked all over the web and found people trying to do this, but not getting anywhere. There...
0
by: Yunus's Group | last post by:
Yunus's Group May 23, 3:36 pm show options Newsgroups: microsoft.public.dotnet.languages.vb From: "Yunus's Group" <yunusasm...@gmail.com> - Find messages by this author Date: 23 May 2005...
8
by: VB Programmer | last post by:
I would appreciate your assistance on this ASP.NET 2.0 site.... This is the wierd problem: While accessing the built in .NET functions for 'profiling' or 'membership' an error is generated (see...
12
by: keepyourstupidspam | last post by:
Hi, I am writing a windows service. The code runs fine when I start the service when my machine is running but it fails to start automatically when the machine reboots. The code bombs out when...
16
by: Hans Fredrik Nordhaug | last post by:
I'm trying to write to a file in the current directory - no remote files. The subject says it all - I can add that both the directory and the file is wordwritable. This happens on a (quite good)...
3
by: dont_spam_me | last post by:
I'm running an application on an embedded device running Linux (2.6.21 kernel) . I've found that when the CPU load gets really high on the device, output to a file stream sometimes fails. When...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.