La función touch() en PHP es una función incorporada que se usa para establecer el tiempo de acceso y modificación de un archivo específico.
El nombre del archivo cuya hora de acceso y modificación se debe configurar se envía como parámetro junto con la hora a la función touch() y devuelve True en caso de éxito y False en caso de falla. Primero se crea un archivo si no existe.
Sintaxis:
touch(filename, time, atime)
Parámetros usados:
La función touch() en PHP acepta tres parámetros.
- filename : Es un parámetro obligatorio que especifica el nombre del archivo cuya hora de acceso y modificación se debe cambiar.
- time : Es un parámetro opcional que especifica la hora. Por defecto toma la hora actual del sistema.
- atime : Es un parámetro opcional que especifica el tiempo de acceso. De manera predeterminada, toma la hora actual del sistema si no se configuran parámetros.
Valor devuelto:
Devuelve True en caso de éxito y False en caso de error.
Errores y excepciones
- La resolución de tiempo puede diferir de un sistema de archivos a otro, por lo que a veces puede obtener resultados inesperados.
- El parámetro $time en la función touch() tiene un límite futuro de alrededor de 1000000 segundos.
- La función touch() utilizada en un directorio devuelve FALSO e imprime «Permiso denegado» en el sistema de archivos NTFS y FAT.
Ejemplos:
Input : $file_pointer = "gfg.txt"; if (touch($file_pointer)) { echo ("$file_pointer modification time has been set to current system time."); } else { echo ("$file_pointer modification time cannot be changed."); } Output :gfg.txt modification time has been set to current system time. Input : $file_pointer = "gfg.txt"; $time = time() - 18000; if (touch($file_pointer, $time)) { echo ("$file_pointer modification time has been changed to 5 hours in the past."); } else { echo ("$file_pointer modification time cannot be changed."); } Output : gfg.txt modification time has been changed to 5 hours in the past.
Los siguientes programas ilustran la función touch().
Supongamos que hay un archivo llamado «gfg.txt»
Programa 1
<?php $file_pointer = "gfg.txt"; // using touch() function to change the modification // time of a file to current system time if (touch($file_pointer)) { echo ("$file_pointer modification time has been set to current system time."); } else { echo ("$file_pointer modification time cannot be changed."); } ?>
Producción:
gfg.txt modification time has been set to current system time.
Programa 2
<?php $file_pointer = "gfg.txt"; // setting touch time to 5 hours in the past $time = time() - 18000; // using touch() function to change the modification // time of a file to current system time if (touch($file_pointer, $time)) { echo ("$file_pointer modification time has been changed to 5 hours in the past."); } else { echo ("$file_pointer modification time cannot be changed."); } ?>
Producción:
gfg.txt modification time has been changed to 5 hours in the past.
Referencia:
http://php.net/manual/en/function.touch.php
Publicación traducida automáticamente
Artículo escrito por Shubrodeep Banerjee y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA