Arquivo.class.php
6.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
/*
**********************************************************************************
* *
* @package URBEM CNM - Soluções em Gestão Pública *
* @copyright (c) 2013 Confederação Nacional de Municípos *
* @author Confederação Nacional de Municípios *
* *
* O URBEM CNM é um software livre; você pode redistribuí-lo e/ou modificá-lo sob *
* os termos da Licença Pública Geral GNU conforme publicada pela Fundação do *
* Software Livre (FSF - Free Software Foundation); na versão 2 da Licença. *
* *
* Este programa é distribuído na expectativa de que seja útil, porém, *
* SEM NENHUMA GARANTIA; nem mesmo a garantia implícita de COMERCIABILIDADE OU *
* ADEQUAÇÃO A UMA FINALIDADE ESPECÍFICA. Consulte a Licença Pública Geral do GNU *
* para mais detalhes. *
* *
* Você deve ter recebido uma cópia da Licença Pública Geral do GNU "LICENCA.txt" *
* com este programa; se não, escreva para a Free Software Foundation Inc., *
* no endereço 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
* *
**********************************************************************************
*/
?>
<?php
/**
* Classe para trabalhar com arquivos
* Data de Criação: 12/01/2005
* @author Analista/Desenvolvedor: Diego Barbosa Victoria
* @package Framework
* @subpackage Arquivos
$Revision: 4009 $
$Name$
$Author: fernando $
$Date: 2005-12-16 14:18:50 -0200 (Sex, 16 Dez 2005) $
Casos de uso: uc-01.01.00
*/
/**
* Classe para trabalhar com arquivos
* @author Analista/Desenvolvedor: Diego Barbosa Victoria
* @package Framework
* @subpackage Arquivos
*/
class Arquivo
{
/**
* @access Private
* @var Resource
*/
public $reArquivo;
/**
* @access Private
* @var String
*/
public $stTipo;
/**
* @access Private
* @var String
*/
public $stNome;
/**
* @access Private
* @var String
*/
public $stLabel;
/**
* @access Private
* @var String
*/
public $stConteudo;
/**
* @access Private
* @var Object
*/
public $obErro;
/**
* @access Public
* @param String $valor
*/
public function setTipo($valor) { $this->stTipo = $valor; }
/**
* @access Public
* @param String $valor
*/
public function setConteudo($valor) { $this->stConteudo = $valor; }
/**
* @access Public
* @Return String
*/
public function getTipo() { return $this->stTipo; }
/**
* @access Public
* @Return String
*/
public function getConteudo() { return $this->stConteudo; }
/**
* @access Public
* @Return String
*/
public function getDiretorio() { return substr($this->stNome,0,strrpos($this->stNome,basename($this->stNome))); }
/**
* @access Public
* @Return Integer
*/
public function getTamanho() { return file_exists($this->stNome) ? filesize($this->stNome) : ''; }
/**
* @access Public
* @Return String
*/
public function getNomeArquivo() { return (strpos($this->stNome,"/")===false) ? ($this->stNome) : (substr($this->stNome,strrpos($this->stNome,"/")+1,strlen($this->stNome)-strrpos($this->stNome,"/"))); }
/**
* Método Construtor
* @access Private
*/
public function Arquivo($stNome , $stLabel = NULL)
{
$this->stLabel = $stLabel;
$this->stNome = $stNome;
$this->stTipo = "application/force-download";
$this->obErro = new Erro;
$this->stConteudo = null;
}
public function Abrir($stModo)
{
$stModo = strtolower(trim($stModo));
if (!$this->reArquivo = @fopen($this->stNome, $stModo)) {
$this->obErro->setDescricao("Arquivo (".$this->stNome.") não pôde ser aberto.");
}
return $this->obErro;
}
public function Fechar()
{
if (!fclose($this->reArquivo)) {
$this->obErro->setDescricao("Arquivo (".$this->stNome.") não pôde ser fechado.");
}
return $this->obErro;
}
public function Gravar($stModo = 'w+')
{
$this->Abrir( $stModo );
if (!$this->obErro->ocorreu()) {
if ( fwrite($this->reArquivo, $this->stConteudo) === false) {
$this->obErro->setDescricao("Arquivo (".$this->stNome.") não pôde ser escrito.");
}
if (!$this->obErro->ocorreu()) {
$this->Fechar();
}
}
return $this->obErro;
}
public function Ler($stModo = 'r')
{
$this->Abrir( $stModo );
if (!$this->obErro->ocorreu()) {
$this->stConteudo = fread($this->reArquivo, $this->getTamanho() );
$this->Fechar();
}
return $this->obErro;
}
public function Show()
{
header('Content-Description: File Transfer');
header('Content-Type: ' . $this->stTipo);
header('Content-Length: ' . $this->getTamanho() );
// se label estiver setado, passa como nome do arquivo de download
if (!is_null($this->stLabel)) {
header('Content-Disposition: attachment; filename=' . $this->stLabel);
} else {
header('Content-Disposition: attachment; filename=' . basename($this->stNome));
}
if ( @readfile($this->stNome) === false ) {
$this->obErro->setDescricao("Erro ao tentar exibir o conteúdo do arquivo (".$this->stNome.").");
}
return $this->obErro;
}
}
?>