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?

藏色散人
Release: 2023-04-05 19:08:01
Original
3954 people have browsed it

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()
Copy after login

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);
Copy after login

Output:

1
Copy after login

Example 2:

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

Output:

GeeksforGeeks
Copy after login

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!

Related labels:
php
Statement of this Website
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template