Connecting Tech Pros Worldwide Forums | Help | Site Map

Creating files using fopen that are unlocked!

monomaniac21
Guest
 
Posts: n/a
#1: Sep 11 '05
Hi everyone

i'm trying to setup my website to create new webpages dynamically using
php but I am have a major problem in that whenever i create a file it
is always created locked so that it can only be read even when i
specify read and write access.

Here is a basic example of the code I am using (I am using a Mac OSX
10.3.9):

<?php

$filename = '/Users/myusername/sites/mysitefolder/newfile.php';
$mytext = 'my text';

fopen($filename, 'w+');
fwrite($filename, $mytext);
fclose($filename);

?>

When i run this program all it does is create a blank file with
read-only access. My question is what settings do I need to change that
will enable me to create writable files using PHP?


monomaniac21
Guest
 
Posts: n/a
#2: Sep 11 '05

re: Creating files using fopen that are unlocked!


I can narrow the problem down a bit more which might help. I can create
a file when I use 'w' or 'w+' even though it is read only. But if try
to create a file such as fopen($filename, 'r') or 'r+' php wont create
it.

Bradley Holt
Guest
 
Posts: n/a
#3: Sep 11 '05

re: Creating files using fopen that are unlocked!


Try changing the permissions on the file. You probably have permissions
to create files in the directory, however, the default permissions on
the file created is probably not enough for you to edit it:
http://www.php.net/manual/en/function.chmod.php

--
Bradley Holt <bradley.holt@gmail.com>
http://www.gtalkprofile.com/profile/2.html

Bradley Holt
Guest
 
Posts: n/a
#4: Sep 11 '05

re: Creating files using fopen that are unlocked!


While opening a file with the 'r' or 'r+' option, PHP will not attempt
to create the file. PHP will only attempt to create the file with the
'w' or 'w+' option.

--
Bradley Holt <bradley.holt@gmail.com>
http://www.gtalkprofile.com/profile/2.html

monomaniac21
Guest
 
Posts: n/a
#5: Sep 11 '05

re: Creating files using fopen that are unlocked!


Thanks for the reply bradley. the problem is that i need these files to
be created with the file permissions in place. A funny thing is that
the owner of these files is given as "www" not my username. Do you have
any idea what that could mean?

Bradley Holt
Guest
 
Posts: n/a
#6: Sep 11 '05

re: Creating files using fopen that are unlocked!


Yes, that's a common username I've seen when creating files from
scripts. Unfortunately, you will need to modify the permissions at
least temporarily or else you won't be able to write to the file. You
may be able to get away with chowning the file to your username
instead:
http://www.php.net/manual/en/function.chown.php

--
Bradley Holt <bradley.holt@gmail.com>
http://www.gtalkprofile.com/profile/2.html

monomaniac21
Guest
 
Posts: n/a
#7: Sep 12 '05

re: Creating files using fopen that are unlocked!


Thanks again bradley. i tried chown but it didn't work.

Malcolm Dew-Jones
Guest
 
Posts: n/a
#8: Sep 12 '05

re: Creating files using fopen that are unlocked!


monomaniac21 (marcrice20@msn.com) wrote:
: Hi everyone

: i'm trying to setup my website to create new webpages dynamically using
: php but I am have a major problem in that whenever i create a file it
: is always created locked so that it can only be read even when i
: specify read and write access.

: Here is a basic example of the code I am using (I am using a Mac OSX
: 10.3.9):

: <?php

: $filename = '/Users/myusername/sites/mysitefolder/newfile.php';
: $mytext = 'my text';

: fopen($filename, 'w+');
: fwrite($filename, $mytext);
: fclose($filename);

: ?>

: When i run this program all it does is create a blank file with
: read-only access. My question is what settings do I need to change that
: will enable me to create writable files using PHP?

RTFM for fopen, chmod, and umask.

"Access" controls who can do what operations on the file.

"Mode" tells the open command what your program wants to do right now (and
the "access" controls if you will be allowed to do it).

'w+' does not create a file with write _access_, it opens a file _handle_
in read/write _mode_.

It is perfectly possible to open (and thereby create) a new file using a
file handle in write _mode_, but for the file thus created to have read
only access permissions.

--

This programmer available for rent.
George Chapman
Guest
 
Posts: n/a
#9: Oct 6 '05

re: Creating files using fopen that are unlocked!


In article <1126480739.073139.131450@g47g2000cwa.googlegroups .com>,
marcrice20@msn.com says...[color=blue]
> Thanks again bradley. i tried chown but it didn't work.
>
>[/color]

I'm guessing that SafeMode is enabled on your host. That precludes a
whole suite of otherwise-available functions, including chown.
George Chapman
Guest
 
Posts: n/a
#10: Oct 6 '05

re: Creating files using fopen that are unlocked!


In article <1126466495.057858.47870@z14g2000cwz.googlegroups. com>,
marcrice20@msn.com says...[color=blue]
> Hi everyone
>
> i'm trying to setup my website to create new webpages dynamically using
> php but I am have a major problem in that whenever i create a file it
> is always created locked so that it can only be read even when i
> specify read and write access.
>[/color]

This is probably not quite what you are looking for, but what about an
alternative?

Based on other responses in this thread, it appears that your host has
some restrictions in place, not the least of which is PHP's SafeMode,
which severely limits (1) the whole file creation process/access
process, (2) changing permissions, and (3) running external programs.
Modifying INI settings is affected as well. Do a search for SafeMode in
the PHP manual to get all the specifics.

Meanwhile, here is my take...

Are indivudual files really the best what to do this?

PHP is not really designed to store data in files. Rather, if your host
also provides a MySQL hookup, a better route might be to store your
pages in a database table, rather than individual files. You would then
have a query look up the individual page's HTML code based on, say, a
variable called PAGE_ID, which you could pass via the URL and retrieve
with $_GET['PAGE_ID']

Not the ideal solution for every possible application, I know. Just a
thought.

- GC
Closed Thread