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

How do you get a filename from a handle/resource?

P: n/a
Hi all,

Does anyone know how you can get a filename if all you've got is the
resource (e.g. returned by fopen())?

The reason I ask is that I'd like to use tmpfile() to create a temporary
file (because then it will be automatically deleted should my script
abruptly terminate.) Once I've written my data to this file, I then
need to pass the filename on to an external command, but I can't see any
way to find out what the temporary file is called.

I've tried using fstat(), but this returns everything except the filename.

Any ideas?

Thanks,
Adam.
Oct 9 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Adam Nielsen schreef:
Does anyone know how you can get a filename if all you've got is the
resource (e.g. returned by fopen())?
You can't, unless you write a wrapper which stores the file name.
The reason I ask is that I'd like to use tmpfile() to create a temporary
file (because then it will be automatically deleted should my script
abruptly terminate.)
It depends on what you mean by "abruptly terminate". When your script
crashes or manually interrupted, the temporary file won't be deleted.
JW
Oct 9 '08 #2

P: n/a
Adam Nielsen escribió:
Does anyone know how you can get a filename if all you've got is the
resource (e.g. returned by fopen())?

The reason I ask is that I'd like to use tmpfile() to create a temporary
file (because then it will be automatically deleted should my script
abruptly terminate.) Once I've written my data to this file, I then
need to pass the filename on to an external command, but I can't see any
way to find out what the temporary file is called.

I've tried using fstat(), but this returns everything except the filename.
Use tempnam() instead:

http://es2.php.net/manual/en/function.tempnam.php
--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--
Oct 9 '08 #3

P: n/a
Adam Nielsen wrote:
Hi all,

Does anyone know how you can get a filename if all you've got is the
resource (e.g. returned by fopen())?

The reason I ask is that I'd like to use tmpfile() to create a temporary
file (because then it will be automatically deleted should my script
abruptly terminate.) Once I've written my data to this file, I then
need to pass the filename on to an external command, but I can't see any
way to find out what the temporary file is called.

I've tried using fstat(), but this returns everything except the filename.

Any ideas?

Thanks,
Adam.
You can't. But then the file will be deleted when you fclose() it - and
you should close a file you have open for writing before passing it off
to another program, anyway.

Rather, use tempname() to get a unique name, then open it.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Oct 9 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.