Creating a temporary file in PHP with a specific name and extension

1.3k views Asked by At

I'm trying to create an attachment that gets sent via email with PHP and PHPMailer. I'm not having trouble doing that, but I specifically need to create a file with a .ics extension. I basically want the following to happen:

  1. Create a temporary file with a specific name, ie. My Date.ics
  2. Write a string to that file.
  3. Attach the file and send the email.
  4. Close the file and delete it (via unlinking it).

Unfortunately, I can't get past step 1. Just doing:

$tmpfname = tempnam(sys_get_temp_dir(), 'My Date.ics');
echo $tmpfname;

Gives me my file name followed by six random characters. For example the above might say something like this:

/tmp/My Date.icsnn8B34

I've also gone into /tmp/ and the files aren't actually being deleted after they are unlinked via unlink($tmpfname);

How can I just get a temporary file with a specific extension?

0

There are 0 answers