1

Is there any way through which I can prevent a opened file handle from closing even the PHP finishes the code execution? I wanna store handles in cookies (or some local file) to reuse them in another script (or after refresh) .

PS: I cannot re-open files each time. I need to keep the file/pipe opened for a long time.

Barmar
  • 741,623
  • 53
  • 500
  • 612
Hritik
  • 673
  • 1
  • 6
  • 24
  • Can you add a code example of what you have already tried? – VerySeriousSoftwareEndeavours May 22 '15 at 15:26
  • @McWayWeb I can't find any way to do so. – Hritik May 22 '15 at 15:33
  • 1
    You can't store file handles in cookies. Resources are tied to a specific process. – Barmar May 22 '15 at 15:34
  • 1
    There is no way to keep file handles after script ends. – umka May 22 '15 at 15:35
  • 1
    Almost anything can be stored in session, except resources like file handles or database connections, but in a cookie? That would be a security breach of enormous magnitude. – Mark Baker May 22 '15 at 15:35
  • 1
    But "Execute and terminate" is the PHP web model..... why can't you re-open files each request? – Mark Baker May 22 '15 at 15:36
  • Because it's a pipe. A STDIN pipe & I can't reopen it everytime. There must be anyway to save handlers somewhere. – Hritik May 22 '15 at 15:38
  • @Hritik - There isn't a way to persist a resource. The best option I can suggest is run a daemon process that maintains the connection to that pipe, and your scripts connect to the daemon on each request – Mark Baker May 22 '15 at 16:02
  • That is not how PHP works. Once the process ends then everything is 'cleaned up' / 'freed'. It would help us to provide a more useful answer if you explained what the original task was that you are trying to solve. And why PHP may be useful? – Ryan Vincent May 22 '15 at 16:23

1 Answers1

1

File handles are operating system constructs, they can't be stored in a database, cookie, file, etc and then still be in any way meaningful. In other words, no, you can't store a file handle (or anything else that's classified as a Resource)

I'd recommend finding a better solution, as you almost certainly don't have to hold a file handle open to achieve what you want to do.

GordonM
  • 31,179
  • 15
  • 87
  • 129
  • But they are stored somewhere during execution, then why not after it? – Hritik May 22 '15 at 17:15
  • 1
    Because they're essentially pointers to a file descriptor. Once the file descriptor isn't needed any more it's destroyed and now it's a pointer into a random region of memory that may or may not even be allocated to PHP next time it runs. – GordonM May 22 '15 at 17:26
  • 1
    @Hritik, PHP automatically frees all resources at the end of 'the run' which is at the 'end of the outer script'. This is a 'good thing' for PHP programmers. It can, and will, make 'persistence' awkward. It is a 'trade off' that PHP made. 'Swings and Roundabouts' - no easy answer. – Ryan Vincent May 22 '15 at 17:51