Foro de elhacker.net

Programación => PHP => Mensaje iniciado por: alexiscruz007 en 23 Febrero 2015, 21:33 pm



Título: php scritp para divivir archivo csv o txt
Publicado por: alexiscruz007 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.