Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP Filesystem tmpfile() Function



The PHP Filesystem tmpfile() function is used to create create a temporary file with a unique name in the read-write (w+) mode. This function can return filehandle similar to the one returned by the fopen() function for the new file, or false on failure.

The file is automatically removed when it is closed (for instance, by calling fclose() function or when there are no remaining references to filehandle returned by tmpfile() function), or when the script ends.

Syntax

Below is the syntax of the PHP Filesystem tmpfile() function −

resource tmpfile ( void )

Parameters

The tmpfile() function does not accept any parameters.

Return Value

The function tmpfile() returns filehandle similar to the one returned by the fopen() function for the new file on success, and FALSE on failure.

Caution

If the script terminates unexpectedly, the temporary file might not be removed.

Error/Exception

The PHP stat() function can give an error and a warning message in the following two cases −

  1. The temporary file is immediately erased when the script ends or when fclose() is used to close it.
  2. The tmpfile() method frequently delivers the Boolean False value, but frequently returning a non-Boolean value that evaluates to False.

PHP Version

The tmpfile() function was first introduced as part of core PHP 4 and work well with the PHP 5, PHP 7 and PHP 8.

Example

Here is the simple example to show yoy how the PHP Filesystem tmpfile() function is used create a temporary file.

<?php
   $temp = tmpfile();
   fwrite($temp, "Tutorialspoint!!!!");
   rewind($temp);  // Rewind to start of a file
   echo fread($temp, 1024);  // Read 1k from a file

   fclose($temp);  // it removes the file
?>

Output

Here is the outcome of the following code −

Tutorialspoint!!!!

Example

Here is the another example to show the usage of tmpfile() function to create a temporary file while handling error.

<?php
   $tempFile = tmpfile();

   if ($tempFile) {
      // Write to the temporary file
      fwrite($tempFile, "Hello, World!");

      // Move back to the beginning
      rewind($tempFile);

      // Read the content 
      echo fread($tempFile, 1024);

      // Close and delete the temporary file
      fclose($tempFile);
   } else {
      echo "Failed to create a temporary file.";
   }
?> 

Output

This will produce the following result −

Hello, World!

Example

Here is one more example using the tmpfile() function to generate and serve a downloadable file.

<?php
   // Create a temporary file
   $tempFile = tmpfile();

   if ($tempFile) {
      // Generate some content
      $csvData = "Name,Email\nAmit Sharma,as@example.com\nVijay Chauhan,vc@example.com";

      // Write the CSV data 
      fwrite($tempFile, $csvData);

      // Set headers for a downloadable CSV file
      header('Content-Type: text/csv');
      header('Content-Disposition: attachment; filename="users.csv"');

      // Output the content of the temporary file
      rewind($tempFile);
      fpassthru($tempFile);

      // Close and delete the temporary file
      fclose($tempFile);
   } else {
      echo "Failed to create a temporary file.";
   }
?> 

Output

This will generate the below output −

Name,Email
Amit Sharma,as@example.com
Vijay Chauhan,vc@example.com

Example

Here is one more example to use tmpfile() function for creating a temporary file just for logging data.

<?php
   // Create a temporary file for logging data
   $tempFile = tmpfile();

   if ($tempFile) {
      // Log some data here
      $logMessage = date('Y-m-d H:i:s') . " - User logged in successfully.\n";

      // Write the log message 
      fwrite($tempFile, $logMessage);

      // Read and output the logged data
      rewind($tempFile);
      echo "Logged data:\n";
      echo fread($tempFile, 1024);

      // Close and delete the file
      fclose($tempFile);
   } else {
      echo "Failed to create a temporary file.";
   }
?> 

Output

This will lead to the following output −

Logged data:
2024-06-27 09:50:18 - User logged in successfully.

Summary

The tmpfile() method is a built-in function to create a temporary file. This function is very helpful for serving downloadable files and logging data temporarily.

php_function_reference.htm
Advertisements