473,624 Members | 2,523 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,"Firs t line\r\n");
fwrite($fp,"Sec ond 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 2715
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****@fanetwor ks.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*******@attgl obal.net
=============== ===

Jan 15 '08 #4
On Tue, 15 Jan 2008 06:42:07 -0500, Jerry Stuckle
<js*******@attg lobal.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
5227
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 by generating a sed command and using system to execute it like so: $t = sprintf("%s \"s/<meta http-equiv="Content-Type" content="text/html;
7
2981
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 others don't - The functionality of this will be used in my other script but I want to make sure I know whats happening first as opposed to bastardizing someone else's work). I have worked some of the code out - I figured the what should be...
25
5064
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 possibly recovering from the error). Printing an error message might be difficult in a graphical environment. --
3
8311
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 are a couple of people selling components... but that is not really an option for me... So, I took the plunge and modified the "WebUIValidation.js" file to make it happen... After tracing through file, I figure it out. It was actually pretty...
0
1710
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 12:36:14 -0700 Local: Mon,May 23 2005 3:36 pm Subject: Writing to text file Reply | Reply to Author | Forward | Print | Individual Message | Show original | Remove | Report Abuse
8
2253
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 following 2 examples): ---- EXAMPLE 1 -------- OK --> Dim p As New ProfileCommon OK --> p = Profile.GetProfile(Me.ddlRacer.SelectedItem.Text) FAILS --> Me.lblDebug.Text = p.FirstName.ToString
12
1883
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 it reaches code that tries to access a singleton class. This is the code.
16
4677
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) free hoster in Norway which doesn't use safe mode, running PHP 5.1.6 as the PHP info below shows ... Test it at: http://home.no.net/moldevbk/fopen-test/?mode=w (write - fails) http://home.no.net/moldevbk/fopen-test/?mode=a (append - ok)...
3
2376
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 I turn on exceptions, the what() method from the exception that gets thrown just tells me that there's a basic_ios::clear error, and I've verified that errno is 0. Does anyone have any suggestions about what I can do?
0
8231
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8168
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8672
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8471
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7153
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6107
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4075
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1474
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.