PHP/IIS: File Read/Write OK, File Unlink Denied

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lorenzogordon@hotmail.com

    PHP/IIS: File Read/Write OK, File Unlink Denied

    Hi there,

    I'd greatly appreciate any insights into the following problem:

    I've got PHP running fine on IIS (OS: Server 2003, SP1; IIS: 6.0; PHP:
    4.3.11).

    In PHP, the user uploads a file, which is then processed and the
    contents are inserted into a new file, created in PHP, onto the server.

    This bit works fine, the new file created by PHP is correctly stored.

    Later, once the user confirms the upload and PHP inserts the contents
    of the file into the DB, the code tries to remove the file created in
    PHP moments earlier, via the unlink() function.

    What I see on screen is the following: Permission denied

    I have made sure that the permissions for the Internet account that
    created (and is trying to delete) the file have full permissions over
    this particular folder and the files within it. But the user must have
    permission anyway, because they are able to create this file in the
    first place.

    I've looked at the permissions for the file in PHP, which read: 0666.
    So everthing *appears* ok to me; I can't see where the permission issue
    is coming from.

    Anyone got any ideas?

    TIA,

    Lorenzo.

  • Kimmo Laine

    #2
    Re: PHP/IIS: File Read/Write OK, File Unlink Denied

    <lorenzogordon@ hotmail.com> wrote in message
    news:1143022232 .809686.208670@ i39g2000cwa.goo glegroups.com.. .[color=blue]
    > Hi there,
    >
    > I'd greatly appreciate any insights into the following problem:
    >
    > I've got PHP running fine on IIS (OS: Server 2003, SP1; IIS: 6.0; PHP:
    > 4.3.11).
    >
    > In PHP, the user uploads a file, which is then processed and the
    > contents are inserted into a new file, created in PHP, onto the server.
    >
    > This bit works fine, the new file created by PHP is correctly stored.
    >
    > Later, once the user confirms the upload and PHP inserts the contents
    > of the file into the DB, the code tries to remove the file created in
    > PHP moments earlier, via the unlink() function.
    >
    > What I see on screen is the following: Permission denied
    >
    > I have made sure that the permissions for the Internet account that
    > created (and is trying to delete) the file have full permissions over
    > this particular folder and the files within it. But the user must have
    > permission anyway, because they are able to create this file in the
    > first place.
    >
    > I've looked at the permissions for the file in PHP, which read: 0666.
    > So everthing *appears* ok to me; I can't see where the permission issue
    > is coming from.
    >
    > Anyone got any ideas?[/color]


    This is just a wild guess, but if you open the file with fopen, for example,
    and then leave it open without fclose, and while the file is open you try to
    unlink it, the file is concidered unremovable because it's in use by php.
    Could something like this be the reason?

    --
    "En ole paha ihminen, mutta omenat ovat elinkeinoni." -Perttu Sirviö
    spam@outolempi. net | Gedoon-S @ IRCnet | rot13(xvzzb@bhg byrzcv.arg)


    Comment

    • lorenzogordon@hotmail.com

      #3
      Re: PHP/IIS: File Read/Write OK, File Unlink Denied

      Good question. Here's the function I'm using to write the file, the
      function is returning true, which indicates that the file is being
      closed properly. (I've used this function to perform the identical same
      task on a Unix server, where it works fine, it's just on IIS I'm having
      this problem):

      function WriteFile($File Name, $TextString)
      {

      $File = fopen($FileName , "w");

      if ($File == false)
      {

      return false;

      } else {

      if (flock($File, LOCK_EX))
      {

      fwrite($File, $TextString);
      flock($File, LOCK_UN);
      fclose($File);
      return true;

      } else {

      return false;

      }

      }

      }


      Any other ides?

      lorenzo.

      Comment

      • lorenzogordon@hotmail.com

        #4
        Re: PHP/IIS: File Read/Write OK, File Unlink Denied

        I read here
        (http://groups.google.co.uk/group/mai...c596d1917561f0)
        about an issue on a Windows server that caused the same problem I'm
        experiencing, because the file had recently been used by IIS.
        I tried add sleep(20), but that made no difference.

        If anyone has any suggestions, please post them here.

        Cheers,
        Lorenzo.

        Comment

        • lorenzogordon@hotmail.com

          #5
          Re: PHP/IIS: File Read/Write OK, File Unlink Denied

          I read here
          (http://groups.google.co.uk/group/mai...c596d1917561f0)
          about an issue on a Windows server that caused the same problem I'm
          experiencing, because the file had recently been used by IIS.
          I tried add sleep(20), but that made no difference.

          If anyone has any suggestions, please post them here.

          Cheers,
          Lorenzo.

          Comment

          • Chung Leong

            #6
            Re: PHP/IIS: File Read/Write OK, File Unlink Denied

            lorenzogordon@h otmail.com wrote:[color=blue]
            > Hi there,
            >
            > I'd greatly appreciate any insights into the following problem:
            >
            > I've got PHP running fine on IIS (OS: Server 2003, SP1; IIS: 6.0; PHP:
            > 4.3.11).
            >
            > In PHP, the user uploads a file, which is then processed and the
            > contents are inserted into a new file, created in PHP, onto the server.
            >
            > This bit works fine, the new file created by PHP is correctly stored.
            >
            > Later, once the user confirms the upload and PHP inserts the contents
            > of the file into the DB, the code tries to remove the file created in
            > PHP moments earlier, via the unlink() function.
            >
            > What I see on screen is the following: Permission denied
            >
            > I have made sure that the permissions for the Internet account that
            > created (and is trying to delete) the file have full permissions over
            > this particular folder and the files within it. But the user must have
            > permission anyway, because they are able to create this file in the
            > first place.
            >
            > I've looked at the permissions for the file in PHP, which read: 0666.
            > So everthing *appears* ok to me; I can't see where the permission issue
            > is coming from.
            >
            > Anyone got any ideas?
            >
            > TIA,
            >
            > Lorenzo.[/color]

            Is the file saved in a web-accessible folder? IIS could be indexing the
            new file.

            Comment

            • lorenzogordon@hotmail.com

              #7
              Re: PHP/IIS: File Read/Write OK, File Unlink Denied

              Sorry, I'm not so hot on IIS, can you explain what IIS does if it's
              indexing the file?

              The folder is outside the web root, so no it's not web-accessible.
              What are the implications of this?

              Thanks,
              Lorenzo.

              Comment

              • Steve

                #8
                Re: PHP/IIS: File Read/Write OK, File Unlink Denied

                [color=blue]
                > I read here
                > (http://groups.google.co.uk/group/mai...c596d1917561f0)
                > about an issue on a Windows server that caused the same problem I'm
                > experiencing, because the file had recently been used by IIS.
                > I tried add sleep(20), but that made no difference.
                >
                > If anyone has any suggestions, please post them here.[/color]

                This is a known problem with IIS. You can't unlink/delete a file that
                has been created during the lifetime of an IIS managed session -
                whether that file was created using PHP, vbscript or whatever.

                Your workaround is either to use throwaway files, that will all be
                unlinked when the server is next restarted; or use some other
                persistence method such as a database. Seek further info from an
                appropriate comp.database.* group.

                ---
                Steve

                Comment

                • lorenzogordon@hotmail.com

                  #9
                  Re: PHP/IIS: File Read/Write OK, File Unlink Denied

                  Thanks for the info, Steve.

                  What counts as a managed session; until IIS is re-started, or until the
                  connection with that particular client is terminated?

                  (What I'm wondering is whetehr I can clean-up the files a day later,
                  once the user has logged off, even if IIS has not been re-started?)

                  Cheers,
                  Lorenzo.

                  Comment

                  • Steve

                    #10
                    Re: PHP/IIS: File Read/Write OK, File Unlink Denied

                    [color=blue]
                    > What counts as a managed session; until IIS is re-started, or until the
                    > connection with that particular client is terminated?[/color]
                    [color=blue]
                    > (What I'm wondering is whetehr I can clean-up the files a day later,
                    > once the user has logged off, even if IIS has not been re-started?)[/color]
                    [color=blue]
                    >From memory you can issue the delete requests, but the files are only[/color]
                    removed when IIS is stopped.

                    ---
                    Steve

                    Comment

                    • lorenzogordon@hotmail.com

                      #11
                      Re: PHP/IIS: File Read/Write OK, File Unlink Denied

                      Thanks Steve.

                      I just ran a little test. I uploaded a file, then an hour later (with
                      the same browser window) I ran the unlink code and it worked.

                      I guess IIS needs mroe than 20 seconds (the length I set the sleep
                      function for) to give-up the file.

                      Lorenzo.

                      Comment

                      Working...