
- PHP - Home
- PHP - Roadmap
- PHP - Introduction
- PHP - Installation
- PHP - History
- PHP - Features
- PHP - Syntax
- PHP - Hello World
- PHP - Comments
- PHP - Variables
- PHP - Echo/Print
- PHP - var_dump
- PHP - $ and $$ Variables
- PHP - Constants
- PHP - Magic Constants
- PHP - Data Types
- PHP - Type Casting
- PHP - Type Juggling
- PHP - Strings
- PHP - Boolean
- PHP - Integers
- PHP - Files & I/O
- PHP - Maths Functions
- PHP - Heredoc & Nowdoc
- PHP - Compound Types
- PHP - File Include
- PHP - Date & Time
- PHP - Scalar Type Declarations
- PHP - Return Type Declarations
- PHP - Operators
- PHP - Arithmetic Operators
- PHP - Comparison Operators
- PHP - Logical Operators
- PHP - Assignment Operators
- PHP - String Operators
- PHP - Array Operators
- PHP - Conditional Operators
- PHP - Spread Operator
- PHP - Null Coalescing Operator
- PHP - Spaceship Operator
- PHP Control Statements
- PHP - Decision Making
- PHP - If…Else Statement
- PHP - Switch Statement
- PHP - Loop Types
- PHP - For Loop
- PHP - Foreach Loop
- PHP - While Loop
- PHP - Do…While Loop
- PHP - Break Statement
- PHP - Continue Statement
- PHP Arrays
- PHP - Arrays
- PHP - Indexed Array
- PHP - Associative Array
- PHP - Multidimensional Array
- PHP - Array Functions
- PHP - Constant Arrays
- PHP Functions
- PHP - Functions
- PHP - Function Parameters
- PHP - Call by value
- PHP - Call by Reference
- PHP - Default Arguments
- PHP - Named Arguments
- PHP - Variable Arguments
- PHP - Returning Values
- PHP - Passing Functions
- PHP - Recursive Functions
- PHP - Type Hints
- PHP - Variable Scope
- PHP - Strict Typing
- PHP - Anonymous Functions
- PHP - Arrow Functions
- PHP - Variable Functions
- PHP - Local Variables
- PHP - Global Variables
- PHP Superglobals
- PHP - Superglobals
- PHP - $GLOBALS
- PHP - $_SERVER
- PHP - $_REQUEST
- PHP - $_POST
- PHP - $_GET
- PHP - $_FILES
- PHP - $_ENV
- PHP - $_COOKIE
- PHP - $_SESSION
- PHP File Handling
- PHP - File Handling
- PHP - Open File
- PHP - Read File
- PHP - Write File
- PHP - File Existence
- PHP - Download File
- PHP - Copy File
- PHP - Append File
- PHP - Delete File
- PHP - Handle CSV File
- PHP - File Permissions
- PHP - Create Directory
- PHP - Listing Files
- Object Oriented PHP
- PHP - Object Oriented Programming
- PHP - Classes and Objects
- PHP - Constructor and Destructor
- PHP - Access Modifiers
- PHP - Inheritance
- PHP - Class Constants
- PHP - Abstract Classes
- PHP - Interfaces
- PHP - Traits
- PHP - Static Methods
- PHP - Static Properties
- PHP - Namespaces
- PHP - Object Iteration
- PHP - Encapsulation
- PHP - Final Keyword
- PHP - Overloading
- PHP - Cloning Objects
- PHP - Anonymous Classes
- PHP Web Development
- PHP - Web Concepts
- PHP - Form Handling
- PHP - Form Validation
- PHP - Form Email/URL
- PHP - Complete Form
- PHP - File Inclusion
- PHP - GET & POST
- PHP - File Uploading
- PHP - Cookies
- PHP - Sessions
- PHP - Session Options
- PHP - Sending Emails
- PHP - Sanitize Input
- PHP - Post-Redirect-Get (PRG)
- PHP - Flash Messages
- PHP AJAX
- PHP - AJAX Introduction
- PHP - AJAX Search
- PHP - AJAX XML Parser
- PHP - AJAX Auto Complete Search
- PHP - AJAX RSS Feed Example
- PHP XML
- PHP - XML Introduction
- PHP - Simple XML Parser
- PHP - SAX Parser Example
- PHP - DOM Parser Example
- PHP Login Example
- PHP - Login Example
- PHP - Facebook Login
- PHP - Paypal Integration
- PHP - MySQL Login
- PHP Advanced
- PHP - MySQL
- PHP.INI File Configuration
- PHP - Array Destructuring
- PHP - Coding Standard
- PHP - Regular Expression
- PHP - Error Handling
- PHP - Try…Catch
- PHP - Bugs Debugging
- PHP - For C Developers
- PHP - For PERL Developers
- PHP - Frameworks
- PHP - Core PHP vs Frame Works
- PHP - Design Patterns
- PHP - Filters
- PHP - JSON
- PHP - Exceptions
- PHP - Special Types
- PHP - Hashing
- PHP - Encryption
- PHP - is_null() Function
- PHP - System Calls
- PHP - HTTP Authentication
- PHP - Swapping Variables
- PHP - Closure::call()
- PHP - Filtered unserialize()
- PHP - IntlChar
- PHP - CSPRNG
- PHP - Expectations
- PHP - Use Statement
- PHP - Integer Division
- PHP - Deprecated Features
- PHP - Removed Extensions & SAPIs
- PHP - PEAR
- PHP - CSRF
- PHP - FastCGI Process
- PHP - PDO Extension
- PHP - Built-In Functions
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 −
- The temporary file is immediately erased when the script ends or when fclose() is used to close it.
- 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.