Hướng dẫn php create temp file - php tạo tệp tạm thời

(Php 4, Php 5, Php 7, Php 8)

TMPFILE - Tạo một tệp tạm thờiCreates a temporary file

Sự mô tả

tmpfile (): tài nguyên | sai(): resource|false

Tệp được tự động xóa khi đóng (ví dụ: bằng cách gọi fclose () hoặc khi không có tài liệu tham khảo còn lại cho xử lý tệp được trả về bởi tmpfile ()) hoặc khi tập lệnh kết thúc.fclose(), or when there are no remaining references to the file handle returned by tmpfile()), or when the script ends.

Thận trọng

Nếu tập lệnh chấm dứt bất ngờ, tệp tạm thời có thể không bị xóa.

Thông số

Chức năng này không có tham số.

Trả về giá trị

Trả về một tay cầm tệp, tương tự như tệp được trả về bởi fopen (), cho tệp mới hoặc false về lỗi.fopen(), for the new file or false on failure.

Ví dụ

Ví dụ #1 tmpfile () ví dụtmpfile() example

$temp tmpfile();
fwrite($temp"writing to tempfile");
fseek($temp0);
echo 
fread($temp1024);
fclose($temp); // this removes the file
?>

Ví dụ trên sẽ xuất ra:

Xem thêm

  • Tempnam () - Tạo tệp có tên tệp duy nhất
  • SYS_GET_TEMP_DIR () - Trả về đường dẫn thư mục được sử dụng cho các tệp tạm thời

Giám mục ¶

4 năm trước

To get the underlying file path of a tmpfile file pointer:

$file = tmpfile();
$path = stream_get_meta_data($file)['uri']; // eg: /tmp/phpFx0513a

Chris [at] PureFormSolutions [dot] com ¶

17 năm trước

I found this function useful when uploading a file through FTP. One of the files I was uploading was input from a textarea on the previous page, so really there was no "file" to upload, this solved the problem nicely:

    # Upload setup.inc
   
$fSetup = tmpfile();
   
fwrite($fSetup,$setup);
   
fseek($fSetup,0);
    if (!
ftp_fput($ftp,"inc/setup.inc",$fSetup,FTP_ASCII)) {
        echo
"
Setup file NOT inserted

"
;
    }
   
fclose($fSetup);
?>

The $setup variable is the contents of the textarea.

And I'm not sure if you need the fseek($temp,0); in there either, just leave it unless you know it doesn't effect it.

Ẩn danh ¶

6 năm trước

Since this function may not be working in some environments, here is a simple workaround:

$temp tmpfile();
fwrite($temp"writing to tempfile");
fseek($temp0);
echo 
fread($temp1024);
fclose($temp); // this removes the file
?>
0

$temp tmpfile();
fwrite($temp"writing to tempfile");
fseek($temp0);
echo 
fread($temp1024);
fclose($temp); // this removes the file
?>
1

$temp tmpfile();
fwrite($temp"writing to tempfile");
fseek($temp0);
echo 
fread($temp1024);
fclose($temp); // this removes the file
?>
2

$temp tmpfile();
fwrite($temp"writing to tempfile");
fseek($temp0);
echo 
fread($temp1024);
fclose($temp); // this removes the file
?>
3

thần thánh76 tại gmail dot com ¶

2 năm trước

$temp tmpfile();
fwrite($temp"writing to tempfile");
fseek($temp0);
echo 
fread($temp1024);
fclose($temp); // this removes the file
?>
5

$temp tmpfile();
fwrite($temp"writing to tempfile");
fseek($temp0);
echo 
fread($temp1024);
fclose($temp); // this removes the file
?>
6

$temp tmpfile();
fwrite($temp"writing to tempfile");
fseek($temp0);
echo 
fread($temp1024);
fclose($temp); // this removes the file
?>
7

Elm tại gmail dot nospamplease dot com ¶

3 năm trước

$temp tmpfile();
fwrite($temp"writing to tempfile");
fseek($temp0);
echo 
fread($temp1024);
fclose($temp); // this removes the file
?>
9

oremanj tại gmail dot com

15 năm trước

To get the underlying file path of a tmpfile file pointer:0

To get the underlying file path of a tmpfile file pointer:1

To get the underlying file path of a tmpfile file pointer:2

ssandor ¶

9 năm trước

To get the underlying file path of a tmpfile file pointer:4