Creating files using fopen that are unlocked!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • monomaniac21

    Creating files using fopen that are unlocked!

    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($filenam e, $mytext);
    fclose($filenam e);

    ?>

    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

    #2
    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.

    Comment

    • Bradley  Holt

      #3
      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:


      --
      Bradley Holt <bradley.holt@g mail.com>


      Comment

      • Bradley  Holt

        #4
        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@g mail.com>


        Comment

        • monomaniac21

          #5
          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?

          Comment

          • Bradley  Holt

            #6
            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:


            --
            Bradley Holt <bradley.holt@g mail.com>


            Comment

            • monomaniac21

              #7
              Re: Creating files using fopen that are unlocked!

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

              Comment

              • Malcolm Dew-Jones

                #8
                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($filenam e, $mytext);
                : fclose($filenam e);

                : ?>

                : 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.

                Comment

                • George Chapman

                  #9
                  Re: Creating files using fopen that are unlocked!

                  In article <1126480739.073 139.131450@g47g 2000cwa.googleg roups.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.

                  Comment

                  • George Chapman

                    #10
                    Re: Creating files using fopen that are unlocked!

                    In article <1126466495.057 858.47870@z14g2 000cwz.googlegr oups.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

                    Comment

                    Working...