Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

tmpfile()

Example

Create a temporary file with a unique name in read-write mode (w+).

<?php
$temp = tmpfile();

fwrite($temp, “Testing, testing.”);
//Rewind to the start of file
rewind($temp);
//Read 1k from file
echo fread($temp,1024);

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

The result of the code above will be:

Testing, testing.

Definition and Usage

The tmpfile() function creates a temporary file with a unique name in read-write (w+) mode.

Note: The file is automatically deleted when closed using fclose() or when the script terminates.

 

Tip: Also consider using the tempnam() function.

Syntax

tmpfile()

Technical Details

 

Return Value:

Returns a file handle (similar to the one returned by fopen()) for the new file; FALSE on failure.

PHP Version:

4.0+