informatica:programacion:php:snippets:ficheros
Diferencias
Muestra las diferencias entre dos versiones de la página.
| Ambos lados, revisión anteriorRevisión previaPróxima revisión | Revisión previa | ||
| informatica:programacion:php:snippets:ficheros [2021/02/03 15:57] – [Escritura] tempwin | informatica:programacion:php:snippets:ficheros [2022/12/26 16:32] (actual) – tempwin | ||
|---|---|---|---|
| Línea 1: | Línea 1: | ||
| ====== PHP Snippets: Ficheros ====== | ====== PHP Snippets: Ficheros ====== | ||
| + | |||
| + | ===== Contar líneas ===== | ||
| + | |||
| + | Para contar las líneas de un fichero de texto: | ||
| + | |||
| + | <code php> | ||
| + | $file = new \SplFileObject(' | ||
| + | $file-> | ||
| + | |||
| + | echo $file-> | ||
| + | </ | ||
| ===== CSV ===== | ===== CSV ===== | ||
| + | |||
| + | * [[https:// | ||
| ==== Escritura ==== | ==== Escritura ==== | ||
| Línea 8: | Línea 21: | ||
| <code php> | <code php> | ||
| - | $fichero = __DIR__ . DIRECTORY_SEPARATOR . "procesos_voa_empresa.csv"; | + | $fichero = __DIR__ . DIRECTORY_SEPARATOR . "mi_fichero.csv"; |
| $fp = fopen($fichero, | $fp = fopen($fichero, | ||
| $cabecera = [ | $cabecera = [ | ||
| - | columna1, | + | |
| - | columna2, | + | |
| - | columna3 | + | |
| ]; | ]; | ||
| Línea 30: | Línea 43: | ||
| </ | </ | ||
| - | El fichero resultante tendrá los campos separados por '','', | + | El fichero resultante tendrá los campos separados por '','', |
| < | < | ||
| Línea 39: | Línea 52: | ||
| </ | </ | ||
| - | === Lectura === | + | ==== Lectura |
| <code php> | <code php> | ||
| $fichero = __DIR__ . DIRECTORY_SEPARATOR . " | $fichero = __DIR__ . DIRECTORY_SEPARATOR . " | ||
| - | $fp = fopen($fichero, | + | $fp = fopen($fichero, |
| while (($fila = fgetcsv($fp, | while (($fila = fgetcsv($fp, | ||
informatica/programacion/php/snippets/ficheros.1612364277.txt.gz · Última modificación: por tempwin
