Home >Backend Development >PHP Tutorial >How does PHP create temporary files in read-write (w+) mode?

How does PHP create temporary files in read-write (w+) mode?

藏色散人
藏色散人Original
2019-04-04 14:38:513878browse

The tmpfile() function in PHP is a built-in function used to create a temporary file with a unique name in read-write (w) mode.

Files created using the tmpfile() function are automatically deleted when closed using fclose() or when there are no remaining references to the file handle.

The temporary file created using the tmpfile() function will also be deleted at the end of the script.

tmpfile()The function does not accept any parameters, it returns a file handle similar to the file handle returned by fopen(), for new files or returns FALSE on failure.

Syntax:

tmpfile()

Return value:

Returns the file handle of the new file on success, and returns FALSE on failure.

Errors And Exception:

1. When the temporary file is closed using fclose(), or when the script ends, the temporary file will be automatically deleted.

2. The function of the tmpfile() function is to return a Boolean value False, but many times it returns a non-Boolean value, and the calculation result of this value is False.

Example 1:

<?php 
$temp_pointer = tmpfile(); 
  
//写入临时文件
fwrite($temp_pointer, &#39;temporary data&#39;); 
  
// 将删除文件
fclose(temp_pointer);

Output:

1

Example 2:

<?php 
$temp_pointer = tmpfile(); 
  
//写入临时文件
fwrite($temp_pointer, "GeeksforGeeks"); 
  
echo fread($temp_pointer, 2048); 
  
fclose($temp_pointer);

Output:

GeeksforGeeks

Related recommendations: "PHP Tutorial"

The above is the detailed content of How does PHP create temporary files in read-write (w+) mode?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn