FileDownload.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. defined('BASEPATH') or exit('No direct script access allowed');
  3. class FileDownload extends CI_Controller
  4. {
  5. protected $rutaDiaria = FCPATH . "DOWNLOADS/DAILY/";
  6. protected $rutaMensual = FCPATH . "DOWNLOADS/MONTH/";
  7. protected $enlaceAtom = "";
  8. public function __construct()
  9. {
  10. parent::__construct();
  11. $this->load->library('Feed');
  12. $this->load->helper('file');
  13. $this->load->helper('xml');
  14. }
  15. public function index()
  16. {
  17. $files = get_filenames($this->rutaDiaria, true);
  18. foreach ($files as $file) {
  19. $entrada = file_get_contents($file);
  20. $xml = new SimpleXMLElement($entrada);
  21. $deletedEntrys = $xml->children('at', true);
  22. $result1 = $xml->entry->children('cac-place-ext', true);
  23. $result2 = $xml->entry->children('cac-place-ext', true)->ContractFolderStatus->children('cac-place-ext', true);
  24. $result3 = $xml->entry->children('cac-place-ext', true)->ContractFolderStatus->children('cac', true);
  25. $result4 = $xml->entry->children('cac-place-ext', true)->ContractFolderStatus->children('cbc', true);
  26. }
  27. }
  28. private function downloadProcess()
  29. {
  30. $fileName = basename(URL_DIARIA);
  31. $this->downloadFile($fileName, URL_DIARIA, $this->rutaDiaria);
  32. $archivoAtom = $this->getFile($this->rutaDiaria . $fileName);
  33. $nuevoAtomAttrNext = $this->readNextAttribute($archivoAtom);
  34. while ($nuevoAtomAttrNext !== "") {
  35. $fileName = basename($nuevoAtomAttrNext);
  36. $this->downloadFile($fileName, $nuevoAtomAttrNext, $this->rutaDiaria);
  37. $archivoAtom = $this->getFile($this->rutaDiaria . $fileName);
  38. $nuevoAtomAttrNext = $this->readNextAttribute($archivoAtom);
  39. }
  40. }
  41. private function getFile($url = URL_DIARIA)
  42. {
  43. $feed = implode(file($url));
  44. $xml = simplexml_load_string($feed);
  45. $json = json_encode($xml);
  46. $array = json_decode($json, true);
  47. return $array;
  48. }
  49. private function readNextAttribute($archivo)
  50. {
  51. $enlace = "";
  52. foreach ($archivo['link'] as $row) {
  53. $attrNext = $row['@attributes']['rel'];
  54. $attrHref = $row['@attributes']['href'];
  55. if ($attrNext === "next") {
  56. $enlace = $attrHref;
  57. }
  58. }
  59. return $enlace;
  60. }
  61. private function newFolder($rutaFinal)
  62. {
  63. $result = false;
  64. if (!is_dir($rutaFinal)) {
  65. $result = mkdir($rutaFinal);
  66. }
  67. return $result;
  68. }
  69. private function downloadFile($fileName, $url, $ruta)
  70. {
  71. if (file_put_contents($ruta . $fileName, file_get_contents($url))) {
  72. return true;
  73. } else {
  74. return false;
  75. }
  76. }
  77. }