<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Create file with unique file name</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.symlink.html">symlink</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.tmpfile.html">tmpfile</a></div> <div class="up"><a href="ref.filesystem.html">Filesystem Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.tempnam" class="refentry"> <div class="refnamediv"> <h1 class="refname">tempnam</h1> <p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">tempnam</span> — <span class="dc-title">Create file with unique file name</span></p> </div> <div class="refsect1 description" id="refsect1-function.tempnam-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">string</span> <span class="methodname"><strong>tempnam</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$dir</code></span> , <span class="methodparam"><span class="type">string</span> <code class="parameter">$prefix</code></span> )</div> <p class="para rdfs-comment"> Creates a file with a unique filename, with access permission set to 0600, in the specified directory. If the directory does not exist or is not writable, <span class="function"><strong>tempnam()</strong></span> may generate a file in the system's temporary directory, and return the full path to that file, including its name. </p> </div> <div class="refsect1 parameters" id="refsect1-function.tempnam-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <code class="parameter">dir</code></dt> <dd> <p class="para"> The directory where the temporary filename will be created. </p> </dd> <dt> <code class="parameter">prefix</code></dt> <dd> <p class="para"> The prefix of the generated temporary filename. </p> <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara"> Windows uses only the first three characters of prefix. </span> </p></blockquote> </dd> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.tempnam-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the new temporary filename (with path), or <strong><code>FALSE</code></strong> on failure. </p> </div> <div class="refsect1 changelog" id="refsect1-function.tempnam-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>4.0.3</td> <td> This function's behavior changed in 4.0.3. The temporary file is also created to avoid a race condition where the file might appear in the filesystem between the time the string was generated and before the script gets around to creating the file. Note, that you need to remove the file in case you need it no more, it is not done automatically. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.tempnam-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-2935"> <p><strong>Example #1 <span class="function"><strong>tempnam()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$tmpfname </span><span style="color: #007700">= </span><span style="color: #0000BB">tempnam</span><span style="color: #007700">(</span><span style="color: #DD0000">"/tmp"</span><span style="color: #007700">, </span><span style="color: #DD0000">"FOO"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$handle </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #0000BB">$tmpfname</span><span style="color: #007700">, </span><span style="color: #DD0000">"w"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">, </span><span style="color: #DD0000">"writing to tempfile"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// do here something<br /><br /></span><span style="color: #0000BB">unlink</span><span style="color: #007700">(</span><span style="color: #0000BB">$tmpfname</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 notes" id="refsect1-function.tempnam-notes"> <h3 class="title">Notes</h3> <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara"> If PHP cannot create a file in the specified <code class="parameter">dir</code> parameter, it falls back on the system default. On NTFS this also happens if the specified <code class="parameter">dir</code> contains more than 65534 files. </span> </p></blockquote> </div> <div class="refsect1 seealso" id="refsect1-function.tempnam-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><span class="function"><a href="function.tmpfile.html" class="function" rel="rdfs-seeAlso">tmpfile()</a> - Creates a temporary file</span></li> <li class="member"><span class="function"><a href="function.sys-get-temp-dir.html" class="function" rel="rdfs-seeAlso">sys_get_temp_dir()</a> - Returns directory path used for temporary files</span></li> <li class="member"><span class="function"><a href="function.unlink.html" class="function" rel="rdfs-seeAlso">unlink()</a> - Deletes a file</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.symlink.html">symlink</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.tmpfile.html">tmpfile</a></div> <div class="up"><a href="ref.filesystem.html">Filesystem Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>