Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. {"usuarios":{"username":"Lucas","data":"30/04/19"}},{"usuarios":{"username":"csa","data":"30/04/19"}}
  2.  
  3. <?php
  4.  
  5. private $path;
  6.  
  7. public function __construct()
  8. {
  9. $path = dirname(__FILE__) . '\';
  10. $this->path = $path;
  11. }
  12.  
  13. public function buscaDados($arq){
  14.  
  15. if(empty($arq)):
  16. return 'Erro parametros passados são invalidos';
  17. endif;
  18.  
  19. $arq = $this->path . $arq . '.json';
  20.  
  21.  
  22. if(!file_exists($arq)):
  23.  
  24. return 'Erro arquivo '.$arq.' não pode ser lido ou não exite';
  25.  
  26. endif;
  27.  
  28. $arq = file_get_contents($arq);
  29.  
  30. $arqArray = json_decode($arq, true);
  31.  
  32. return $arqArray;
  33. }
  34.  
  35.  
  36. public function salvarDados($d, $i,$arq){
  37.  
  38.  
  39. if(empty($d) & empty($i) & empty($arq)):
  40.  
  41. return 'Erro Parametros passados são invalidos';
  42.  
  43. endif;
  44.  
  45.  
  46.  
  47. $arq = $this->path . $arq . '.json';
  48.  
  49.  
  50. if (!file_exists($arq)):
  51. return 'O arquivo '.$arq .' a ser modificado não exite';
  52. endif;
  53.  
  54.  
  55.  
  56. // Adiciona o identificador
  57. $dados = array($i => $d);
  58.  
  59. // Tranforma o array $dados_identificador em JSON
  60. $dados_json = json_encode($dados);
  61.  
  62. // "a" indicar que o arquivo é aberto para ser escrito
  63. $fp = fopen($arq, "a");
  64.  
  65. // Escreve o conteúdo JSON no arquivo
  66. $escreve = fwrite($fp, $dados_json);
  67.  
  68. // Fecha o arquivo
  69. fclose($fp);
  70. return true;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement