elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Únete al Grupo Steam elhacker.NET


  Mostrar Temas
Páginas: [1]
1  Programación / PHP / php scritp para borrar las ultimas 5 lineas de un archivo csv o txt en: 23 Febrero 2015, 21:40 pm
Hola amigos necesito en  script que me borre las ultimas 5 lineas de un archivo csv.

He intentado con este codigo

Código
  1. if($_POST['validacion']=="si"){  
  2.    $archivo = './datos.txt';  
  3.    if(file_exists($archivo)) {  
  4.        $file = fopen($archivo,'r');  
  5.        while(!feof($file)) {  
  6.            $name = fgets($file);  
  7.            $lineas[] = $name;  
  8.        }  
  9.        fclose($file);  
  10.  
  11.        // Todas las lineas quedan almacenadas en $lineas  
  12.        // Ahora eliminas la fila 15 por ejemplo, en el array sería la posicion 14 (empezamos por la 0)  
  13.        unset($lineas[14]);  
  14.        $lineas = array_values($lineas);  
  15.        print_r($lineas);  
  16.        // GUARDAMOS  
  17.        $file = fopen($archivo, "w");  
  18.        foreach( $lineas as $linea ) {  
  19.            fwrite( $file, $linea );  
  20.        }  
  21.        fclose( $file );
  22.    }
  23. }



pero no me funciona.

luego unirlo con este para que haga lo que quiero

Código
  1. <?php
  2. $fichero = "mi_fichero.txt";
  3. $filas = file($fichero);
  4. $ultima_linea = count($filas);
  5. $ultima_linea_escritura = $filas[$ultima_linea];
  6. echo "Aqui esta:<br>";
  7. echo "$ultima_linea_escritura";
  8. ?>


gracias de antemano por su ayuda

Mod: Etiquetas GeSHi obligatorias.
2  Programación / PHP / php scritp para divivir archivo csv o txt en: 23 Febrero 2015, 21:33 pm
Tengo el siguiente codigo para dividir un archivo csv en partes mas chicas, pero el resultado de algunos sale desordenado, no se si es por que el archivo tiene un descripcion larga y tambien usa html y talvez lee algun carater de la archivo y se confunde, gracias de antemano por la ayuda.

Código
  1. <?php
  2. /**
  3. * Split a CSV file
  4. *
  5. * Each row is its own line.
  6. * Each cell is comma-separated
  7. * This file splits it into piece of size $size, add the header row
  8. * and names the resulting file filename_X.csv where filename is the
  9. * name of the original file and X is an incrementing integer.
  10. */
  11.  
  12. // Editable Options
  13. $size = 20000; // about 20kb
  14. $to_read = 'populate.csv';
  15.  
  16. // Do not edit
  17. $done = false;
  18. $part = 0;
  19. if (($handle = fopen($to_read, "r")) !== FALSE) {
  20.    $header = fgets($handle);
  21.    while ($done == false) {
  22.        $locA = ftell($handle); // gets the current location. START
  23.        fseek($handle, $size, SEEK_CUR); // jump the length of $size from current position
  24.        $tmp = fgets($handle); // read to the end of line. We want full lines
  25.        $locB = ftell($handle); // gets the current location. END
  26.        $span = ($locB - $locA);
  27.        fseek($handle, $locA, SEEK_SET); // jump to the START of this chunk
  28.        $chunk = fread($handle,$span); // read the chunk between START and END
  29.        file_put_contents($to_read.'_'.$part.'.csv',$header.$chunk);
  30.        $part++;
  31.        if (strlen($chunk) < $size) $done = true;
  32.    }
  33.    fclose($handle);
  34. }
  35. ?>

Mod: Etiquetas GeSHi obligatorias.
Páginas: [1]
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines