Listar os arquivos de uma pasta Existe algum exemplo de como listar os arquivos de uma pasta e adicionar para seleção em um componente, para visualização? Eu tentei o TSortList, mas não obtive sucesso!...
MS
Listar os arquivos de uma pasta  
Fechado
Existe algum exemplo de como listar os arquivos de uma pasta e adicionar para seleção em um componente, para visualização?
Eu tentei o TSortList, mas não obtive sucesso!

Curso Dominando o Adianti Framework

O material mais completo de treinamento do Framework.
Curso em vídeo aulas + Livro completo + Códigos fontes do projeto ERPHouse.
Conteúdo Atualizado!


Dominando o Adianti Framework Quero me inscrever agora!

Comentários (5)


WJ

espero ter ajudado

 
  1. <?php
  2. $pasta = 'imagens/'; //NOME DA PASTA
  3.  if(is_dir($pasta)) //VERIFICA SE REALMENTE É UM DIRETORIO
  4. {
  5.   $diretorio = dir($pasta); // LE O QUE TEM DENTRO DA PASTA
  6. $item = array();
  7.   while(($arquivo = $diretorio->read()) !== false) //LE ARQUIVO POR ARQUIVO
  8.   {
  9.    $item[$arquivo] = $arquivo; //CRIA A ARRAY COM DADOS QUE IRA SER EXIBIDA NA TSortList
  10.   }
  11.   $diretorio->close();
  12. }
  13.   $lista = new TSortList('nome_do_meu_campo'); //CRIA A TSortList
  14. $lista->addItems( $item );//ADD ARRAY EM LISTA
  15. ?>
LC

Não sei se vai ajudar, mais neste exemplo: www.adianti.com.br/framework_files/tutor/index.php?class=FormInterac

tem TCombo::reload('form_interaction', 'response_b', $options); que alimenta uma combo, talvez de para adaptar ai.
MS

Obrigado,
foi exatamente o que buscava.
LJ

 
  1. <?php
  2. /**
  3. * CompleteDataGridView Listing
  4. *
  5. * @version 1.0
  6. * @package samples
  7. * @subpackage tutor
  8. * @author Pablo Dall'Oglio
  9. * @copyright Copyright (c) 2006-2013 Adianti Solutions Ltd. (http://www.adianti.com.br)
  10. * @license http://www.adianti.com.br/framework-license
  11. */
  12. class PublicarInternoForm extends TPage
  13. {
  14. private $form; // registration form
  15. /**
  16. * Class constructor
  17. * Creates the page, the form and the listing
  18. */
  19. public function __construct()
  20. {
  21. parent::__construct();
  22. // creates the form
  23. $this->form = new TForm('form_Fotografo');
  24. $this->form->class = 'tform'; // CSS class
  25. $this->form->style = 'width: 500px';
  26. // creates a table
  27. $table = new TTable;
  28. $table-> width = '100%';
  29. $this->form->add($table);
  30. // add the table inside the form
  31. // create the form fields
  32. $file = new TCombo('file');
  33. //preenche o combo com os arquivos da pasta
  34. $diretorio = "app/output/";
  35. $arquivos = array();
  36. $i=1;
  37. $ponteiro = opendir($diretorio);
  38. // monta os vetores com os itens encontrados na pasta
  39. while ($nome_itens = readdir($ponteiro)) {
  40. $itens[] = $nome_itens;
  41. }
  42. sort($itens);
  43. // percorre o vetor para fazer a separacao entre arquivos e pastas
  44. foreach ($itens as $listar)
  45. {
  46. // retira "./" e "../" para que retorne apenas pastas e arquivos
  47. if ($listar!="." && $listar!="..")
  48. {
  49. // checa se o tipo de arquivo encontrado é uma pasta
  50. if (is_file($diretorio.$listar))
  51. {
  52. $arquivos[$listar]=$listar;
  53. $i++;
  54. }
  55. }
  56. }
  57. $file->addItems($arquivos);
  58. // add a row for the field nome
  59. $row=$table->addRow();
  60. $row->class = 'tformtitle'; // CSS class
  61. $row->addCell( new TLabel('Publica Arquivos Servidor') )->colspan = 3;
  62. $table->addRowSet( new TLabel('Arquivo: '), $file);
  63. $table->addRowSet( " "," ");
  64. //cria botao de acao
  65. $publica_button = TButton::create('publica', array($this, 'onPublicarInternoSuaFoto'), 'Publicar-SUAfoto', 'ico_print.png');
  66. $mostra_button = TButton::create('mostra', array($this, 'onMostra'), 'Exibir', 'ico_print.png');
  67. $excluir_button = TButton::create('excluir', array($this, 'onExcluir'), 'Excluir', 'ico_print.png');
  68. $download_button = TButton::create('download', array($this, 'onDownload'), 'Download', 'ico_print.png');
  69. $buttons_box = new THBox;
  70. $buttons_box->add($publica_button);
  71. $buttons_box->add($mostra_button);
  72. $buttons_box->add($excluir_button);
  73. $buttons_box->add($download_button);
  74. // add a row for the form actions
  75. $row=$table->addRow();
  76. $row->class = 'tformaction'; // CSS class
  77. $row->addCell($buttons_box)->colspan = 3;
  78. // define wich are the form fields
  79. $this->form->setFields(array($file, $publica_button,$mostra_button,$excluir_button,$download_button));
  80. parent::add($this->form);
  81. }
  82. function onPublicarInternoSuaFoto()
  83. {
  84. try
  85. {
  86. // open a transaction with database 'samples'
  87. TTransaction::open('suafoto');
  88. $conn = TTransaction::get(); //obtem uma conexão
  89. // get the form data into an active record Book
  90. $object = $this->form->getData();
  91. if($object->file<>"")
  92. {
  93. $querie = file_get_contents("app/output/".$object->file);
  94. $result = $conn->query($querie);
  95. new TMessage('info','Banco atualizado com sucesso '.$result->rowCount());
  96. }else{
  97. new TMessage('info','Escolha um arquivo');
  98. }
  99. // close the transaction
  100. TTransaction::close();
  101. }
  102. catch (Exception $e) // in case of exception
  103. {
  104. // shows the exception error message
  105. new TMessage('error', '<b>Error</b> ' . $e->getMessage());
  106. // undo all pending operations
  107. TTransaction::rollback();
  108. }
  109. }
  110. function onMostra()
  111. {
  112. $object = $this->form->getData();
  113. if($object->file<>"")
  114. {
  115. $this->form->setData($object);
  116. $scroll = new TScroll;
  117. $scroll->setSize(640, 460);
  118. $scroll->style = 'padding: 4px; border-radius: 4px;';
  119. $source = new TSourceCode;
  120. $source->loadFile("app/output/".$object->file);
  121. $scroll->add($source);
  122. // wrap the page content
  123. $vbox = new TVBox;
  124. $vbox->add($scroll);
  125. parent::add($vbox);
  126. }
  127. }
  128. function onExcluir()
  129. {
  130. try
  131. {
  132. $object = $this->form->getData();
  133. // define the delete action
  134. //$action = new TAction(unlink("app/output/".$object->file));
  135. //$action->setParameters($param); // pass the key parameter ahead
  136. unlink("app/output/".$object->file);
  137. // shows a dialog to the user
  138. new TMessage('info','Arquivo excluido');
  139. //new TQuestion('Confirma a EXCLUSÃO ?', $action);
  140. }
  141. catch (Exception $e) // in case of exception
  142. {
  143. // shows the exception error message
  144. new TMessage('error', '<b>Error</b> ' . $e->getMessage());
  145. // undo all pending operations
  146. TTransaction::rollback();
  147. }
  148. // preciso de uma forma de recarregar a pagina ou remover este item do $file
  149. }
  150. function onDownload()
  151. {
  152. $object = $this->form->getData();
  153. $file="app/output/".$object->file;
  154. parent::openfile($file);
  155. }
  156. }
  157. ?>
LJ

preciso de uma ajuda na linha 180 , quando apago um arquivo, ele apaga do diretorio, esta correto, porem não sei como recarregar a lista para não aparecer mais, tenho que sair e voltar.