PHP - Descarga de archivos desde directorio

 
Vista:
sin imagen de perfil

Descarga de archivos desde directorio

Publicado por Ezequiel (50 intervenciones) el 14/10/2014 17:26:14
hola, quisiera saber si me pueden ayudar a hacer un bucle para obtener los archivos que tengo guardados en una carpeta y asi poder descargarlos. Tengo el siguiente codigo:
gracias

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
<?php
if (
(
isset($_POST['file']) ||
!empty($_POST['file'])
)
&&
isset($_POST['descargar'])
) {
$file = $_POST['file'];
$file = basename($file);
$file = "archivos/".$file;
if(is_file($file))
{
// requerido para IE
if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off'); }
if (function_exists('mime_content_type')){
$type = mime_content_type($file);
}else if (function_exists('finfo_file')){
$info = finfo_open(FILEINFO_MIME);
$type = finfo_file($info, $file);
finfo_close($info);
 
}else{
switch(strtolower(end(explode('.',$file))))
{
case 'pdf': $type = 'application/pdf'; break;
case 'zip': $type = 'application/zip'; break;
case 'jpeg':
case 'jpg': $type = 'image/jpg'; break;
default: $type = 'application/force-download';
}
}
 
header('Pragma: public'); // required
header('Expires: 0'); // no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($file)).' GMT');
header('Cache-Control: private',false);
header('Content-Type: '.$type);
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($file));
header('Connection: close');
readfile($file);
exit();
}
}
?>
<html>
<head>
<title></title>
</head>
<body>
<!-- ACA SI NO ME EQUIVOCO TENDRIA QUE IR EL BUCLE PARA GENERAR UNA LISTA CON TODOS LOS ARCHIVOS
-->
<div id="archivos">
<form method="POST" action="">
<input type="submit" name="descargar" value="Descargar">
<input type="hidden" name="file" value="">
</form>
</div>
</body>
</html>
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder