f



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.

0
3/22/2006 10:10:32 AM
comp.lang.php 32646 articles. 0 followers. Post Follow

10 Replies
1402 Views

Similar Articles

[PageSpeed] 27

<lorenzogordon@hotmail.com> wrote in message 
news:1143022232.809686.208670@i39g2000cwa.googlegroups.com...
> 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?


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@bhgbyrzcv.arg) 


0
spam4297 (354)
3/22/2006 10:35:14 AM
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($FileName, $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.

0
3/22/2006 11:37:26 AM
I read here
(http://groups.google.co.uk/group/mailing.www.php-dev/browse_frm/thread/f4bc2a7985cc4e/46c596d1917561f0?lnk=st&q=php+windows+permission+denied+unlink&rnum=8&hl=en#46c596d1917561f0)
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.

0
3/22/2006 6:33:08 PM
I read here
(http://groups.google.co.uk/group/mailing.www.php-dev/browse_frm/thread/f4bc2a7985cc4e/46c596d1917561f0?lnk=st&q=php+windows+permission+denied+unlink&rnum=8&hl=en#46c596d1917561f0)
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.

0
3/22/2006 6:33:13 PM
lorenzogordon@hotmail.com wrote:
> 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.

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

0
chernyshevsky (2297)
3/23/2006 1:01:15 AM
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.

0
3/23/2006 9:06:59 AM
> I read here
> (http://groups.google.co.uk/group/mailing.www.php-dev/browse_frm/thread/f4bc2a7985cc4e/46c596d1917561f0?lnk=st&q=php+windows+permission+denied+unlink&rnum=8&hl=en#46c596d1917561f0)
> 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.

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

0
3/23/2006 10:49:05 AM
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.

0
3/23/2006 11:20:17 AM
> 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?)

>From memory you can issue the delete requests, but the files are only
removed when IIS is stopped.

---
Steve

0
3/23/2006 2:37:32 PM
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.

0
3/23/2006 3:33:19 PM
Reply: