473,395 Members | 2,079 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,395 software developers and data experts.

Unable to write a file in a directory that my PHP script creates

Hi.

I've a problem of being able to create and remove a directory but
unable to write a file inside the created directory for some strange
reason. I suspect that this problem could be vaguely linked to Safe
mode being set to On since my site is using shared server hosting and
probably insufficient/incorrect Unix file permission.

Below is my test script that helps me narrow down the problem.
--------------------------------------------------------------------------------------------
<html>
<head>
<title>File IO Test</title>
</head>
<body>
<pre>

<?php
define("TEST_DIR", "./newsletter/2005_11");
define("TEST_FILE", "./newsletter/2005_11/file_io_test.txt");

echo "Make Directory";
if(mkdir(TEST_DIR, 0777)){
echo "<font color=\"green\">OK</font>\n";
}
else{
echo "<font color=\"red\">Fail</font>\n";
}

if(file_exists(TEST_DIR) && is_dir(TEST_DIR)){
echo TEST_DIR . " exists.\n";
}
else{
echo TEST_DIR . " does not exists.\n";
}
echo "<hr>\n";

// TEST FILE WRITE
echo "Write Open File";
$file_handle = fopen(TEST_FILE, "w");
if($file_handle){
echo "OK";
}
else{
echo "Fail";
}
fwrite($file_handle, "This file is written at " . date("Y/m/d H:i:s") .
"\n");
echo "Write Close File";
if(fclose($file_handle)){
echo "<font color=\"green\">OK</font>\n";
}
else{
echo "<font color=\"red\">Fail</font>\n";
}
echo "--- START FILE DUMP ---\n";
readfile(TEST_FILE);
echo "--- END FILE DUMP ---\n";
echo "<hr>";
?>
</body>
</html>
--------------------------------------------------------------------------------------------
This script will report a success with the creation of the directory on
my server, but fails to write a file inside the newly created
directory.

Originally, I only call mkdir without specifying any permission and it
doesn't work. Thus I explicitly instruct mkdir to use 0777 to allow all
write operation by everybody, in case PHP and Apache try to write files
using "nobody" or "www" user account, instead of my shell login user
account. Still it doesn't work. The owner of the created directory is
called "www". When I login via SSH and check the permission, strangely,
the permission is 775 instead, with Write permission for Others
disabled, although I told mkdir to use 0777 instead.

Is there any workaround, and what am I doing wrong here? Thanks for
sharing with me any insight into this problem. I'm not quite sure what
exactly is wrong here, whether because of PHP Safe mode, incorrect
directory permission, incorrect file permission or the case of
different UID or owner.

Goh, Yong Kwang
Singapore

Nov 22 '05 #1
2 4883
>This script will report a success with the creation of the directory on
my server, but fails to write a file inside the newly created
directory.

Originally, I only call mkdir without specifying any permission and it
doesn't work. Thus I explicitly instruct mkdir to use 0777 to allow all
write operation by everybody, in case PHP and Apache try to write files
using "nobody" or "www" user account, instead of my shell login user
account. Still it doesn't work. The owner of the created directory is
called "www". When I login via SSH and check the permission, strangely,
the permission is 775 instead, with Write permission for Others
disabled, although I told mkdir to use 0777 instead.


Look up "umask". mkdir() and file creation are subject to the
umask, which by default doesn't allow world write.

Does the PHP user ("www" in your case) *own* the directory?
If not, and safe mode is ON, really getting mode 777 won't do any good.

Gordon L. Burditt
Nov 22 '05 #2
Hi Gordon. I followed your instruction and things seem to be improving
though not there yet. Basically I set umask(0) before the running mkdir
and it works.

Now the directory shows that it has write permission granted to
everyone. Below is the line I copied after running "ls -la"

drwxrwxrwx 2 www 84 512 Nov 16 21:16 2005_11

It shows that the newly created directory is owned by user "www". And
it has read, write, execute (777) for everyone. But still the file
write operation fails when it tries a fopen("./2005_11/test.txt", "w")

What is still wrong I wonder?

Nov 22 '05 #3

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

Similar topics

1
by: PeterB | last post by:
Hi! I'm using Pure ASP File Upload (http://www.asp101.com/articles/jacob/scriptupload.asp) to upload a file from a client to a server. I am testing both on a local IIS and a remote server. The...
1
by: Joel Finkel | last post by:
Folks, I am unable to execute any ASPX files on a Windows 2000 Server, running as a Primary Domain Controller. The error is "Error BC31019: Unable to write to output file '<filename>'" By...
3
by: hussein | last post by:
'm not bale to run my projec on the 2000 server, he error was: error while trying to run project: unable to start debugging on the web server.erver side error occurred on sending debug HTTP request...
10
by: robwharram | last post by:
Hi, I'm quite frustrated in the fact that I can't even display a simple "Hello World" message on .Net. I've been through all of the groups and searched all over the place and haven't been able...
2
by: Michael | last post by:
Running DB2 v7 UDB ("DB2 v7.1.0.93", "n031208" and "WR21333") on Windows XP, I am unable to find out why the "Build for Debug" option within Stored Procedure Builder is not enabled on Java stored...
0
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted...
3
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted...
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)...
6
Markus
by: Markus | last post by:
I'm adding to my script a section that allows a thumbnail to be created and saved. I get this error: Warning: imagejpeg() : Unable to open '../uploads/thumb/' for writing: Is a directory in...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
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
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.