PHP crea un archivo tmp aleatorio y obtiene su ruta completa


Después de hacerlo:

$temp = tmpfile();
fwrite($temp, "writing to tempfile");

Me gustaría obtener una ruta completa al archivo $temp que tmpfile ha creado.

¿Qué debo hacer para obtener esa información?

Author: GrumpyCrouton, 2013-05-10

3 answers

$path = tempnam(sys_get_temp_dir(), 'prefix');

Ver este ejemplo.

 24
Author: Alix Axel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-10-09 02:40:22

tmpfile devuelve un puntero de archivo. Para obtener la ruta subyacente, necesita preguntar al flujo por sus metadatos :

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

¿El beneficio del enfoque tmpfile? PHP elimina automáticamente el $path cuando $file sale del ámbito. Con tempnam, debe eliminar manualmente el archivo creado.

 32
Author: bishop,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-09-12 12:26:28

Utilizar la función tempnam en su lugar?

 2
Author: Halcyon,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-05-10 16:47:15