# Questions on stream

- Thread: https://externals.io/message/17551
- Original archive: https://news-web.php.net/php.internals/17551
- Started by Rob Richards on 2005-08-05T20:48:21+00:00
- Messages: 4
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #17551 — Rob Richards — 2005-08-05T20:48:21+00:00

URL: https://externals.io/message/17551#17551

I was wondering if the following are bugs or expected behavior/wont change.

Take the following code:
<?php
$handle = fopen("file.txt", "w");
fwrite($handle, "SOME DATA");
unlink("file.txt");
if (fwrite($handle, "SOMEMORE") === FALSE) {
   print "CANNOT WRITE";
} else {
   print "Wrote Data";
}
fclose($handle);
?>

Under linux the file is deleted and the result is Wrote Data (even 
though the last fwrite didnt do anything).
Under windows, the `unlink()` call results in a permissions denied error.
Shouldn't linux not allow the file to be deleted with an open stream as 
well?

Next, URI escpaing:
Stream URIs need to be escaped, but this isn't true with the filesystem.
$test = file_get_contents("t%20e"); // results in error
$test = file_get_contents("t e"); // reads the file "t e"
(same results using full URI file://....)

Why don't filesystem paths have to be escaped but other protocols do?
This is against the RFC as spaces should be escaped.
Behvavior is same on linux and win so not an OS issue.

Rob

---

## #17554 — Antony Dovgal — 2005-08-05T21:01:53+00:00

URL: https://externals.io/message/17551#17554 · In reply to: #17551

On Fri, 05 Aug 2005 16:48:21 -0400
Rob Richards <rrichards@ctindustries.net> wrote:

> I was wondering if the following are bugs or expected behavior/wont change.
> 
> Take the following code:
> <?php
> $handle = fopen("file.txt", "w");
> fwrite($handle, "SOME DATA");
> unlink("file.txt");
> if (fwrite($handle, "SOMEMORE") === FALSE) {
>    print "CANNOT WRITE";
> } else {
>    print "Wrote Data";
> }
> fclose($handle);
> ?>
> 
> Under linux the file is deleted and the result is Wrote Data (even 
> though the last fwrite didnt do anything).

That's because on Linux data gets phisically deleted only when number of 
hardlinks to it and number of open descriptors becomes 0.

It's perfectly fine to open & delete file and continue writing to/reading from it.
That's, btw, one of the well known methods used do get a temporary FD. 

> Under windows, the `unlink()` call results in a permissions denied error.

Yup =) Also on Windows you can't open a file already opened by someone.

> Shouldn't linux not allow the file to be deleted with an open stream as 
> well?

No, why? 

-- 
Wbr, 
Antony Dovgal

---

## #17555 — Rob Richards — 2005-08-05T21:21:09+00:00

URL: https://externals.io/message/17551#17555 · In reply to: #17554

Antony Dovgal wrote:

> > […]
>No, why? 
>  
>
Dunno, kind of the reason I was asking. Just doesn't seem right that you 
can be writing to a file, it gets deleted and you have no idea it was 
deleted seeing that it tells you it did write data to the non-existant file.

Thanks for the explanation though.

Rob

---

## #17556 — Wez Furlong — 2005-08-06T06:08:49+00:00

URL: https://externals.io/message/17551#17556 · In reply to: #17554

> Yup =) Also on Windows you can't open a file already opened by someone.

Actually, you can if the person that opened it says you can, it's just
that it's not the default mode for the file.

--Wez.
