Get temp file php




















Viewed 81k times. Add a comment. Active Oldest Votes. I've used uniqid in the past to generate a unique filename, but not actually create the file.

Lusid Lusid 4, 1 1 gold badge 22 22 silver badges 24 24 bronze badges. I don't know if adding a rand in there will necessarily make it any more random - odds are that both uniqid and rand are being seeded by the system time. This while loop should really be written as a do-while loop.

Show 3 more comments. You check the file system to make sure it doesn't exist. If it does, repeat the previous step. Another, evil, process creates a file with the same name as a hard link to a file Mr Evil wants you to accidentally overwrite. You open the file, thinking you're creating the file rather than opening an existing one in write mode and you start writing to it. You just overwrote something important. If any of the above fails, try again with a different name.

Returns the filename created. Chris Cogdon Chris Cogdon 6, 5 5 gold badges 36 36 silver badges 30 30 bronze badges. Better use Unix timestamp with the user id. Pang 8, gold badges 82 82 silver badges bronze badges. Example 1 tmpfile example. Submit a Pull Request Report a Bug. Description tmpfile : resource false. Or am I wrong? Maybe I can just generate unique uniqID filenames like that, and than delete them. Or can this be too consuming for the server if many people are downloading?

This approach has the benefit of leaving it up to PHP to pick a good place and name for this temporary file, which could end up being a good thing or a bad thing depending on your needs.

But it is the simplest way to do this if you don't yet have a specific reason to pick your own directory and filename. Getting filename or deleting file using file handle. This will give you the directory. I guess after that you are on your own. For newer not very new lol versions of PHP requires php 5. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow.

You must remember to manually delete the temporary file once you are done with it. In addition, once the script execution ends, the temporary file created by tmpfile is automatically deleted. Here is your example script using tempnam instead of tmpfile :.

The file is automatically removed when closed for example, by calling fclose , or when there are no remaining references to the file handle returned by tmpfile , or when the script ends. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more.



0コメント

  • 1000 / 1000