By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,866 Members | 1,704 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,866 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.

Mar 22 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
<lo***********@hotmail.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.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ö
sp**@outolempi.net | Gedoon-S @ IRCnet | rot13(xv***@bhgbyrzcv.arg)
Mar 22 '06 #2

P: n/a
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.

Mar 22 '06 #3

P: n/a
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.

Mar 22 '06 #4

P: n/a
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.

Mar 22 '06 #5

P: n/a
lo***********@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.

Mar 23 '06 #6

P: n/a
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.

Mar 23 '06 #7

P: n/a
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.


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

Mar 23 '06 #8

P: n/a
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.

Mar 23 '06 #9

P: n/a
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

Mar 23 '06 #10

P: n/a
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.

Mar 23 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.