Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I copy a file content into a temporary file in PHP? [duplicate]

I tried this:

$temp = tmpfile();
file_put_contents($temp,file_get_contents("$path/$filename"));

But I get this error: "Warning: file_put_contents() expects parameter 1 to be string,"

If I try:

echo file_get_contents("$path/$filename");

It return to screen the file content as a long string. Where am I wrong?

like image 852
kiks73 Avatar asked May 13 '14 14:05

kiks73


1 Answers

tmpfile() creates a temporary file with a unique name in read-write (w+) mode and returns a file handle to use with fwrite for example.

$temp = tmpfile();
fwrite($temp, file_get_contents("$path/$filename"));

The file is automatically removed when closed (for example, by calling fclose(), or when there are no remaining references to the file handle returned by tmpfile()), or when the script ends. look at php ref.

like image 192
Thiago França Avatar answered Sep 17 '22 12:09

Thiago França