Llevo varias horas recorriendo la red bsucando como poder descargar un archivo desde mi pagina web.. y no hay manera lo unico que consigo es que me abra su contenido e una pagina.... (PK *�C dimension/UX__Ro_R�PK*�Cdimension/.DS_StoreUXo_Ro_R��Mj�0....)
he probado con:
Código PHP:
// accepted file extensions
$extensions = array("pdf","mp3","zip","rar");
// file name to download
$file = $_GET["file"];
// file size
$size = filesize($file);
// Prevent go throught directories
if(strpos($file,"/")!==false){
die("Permission denied to change directory, please, especify only a file name");
}
// test the file estension
$ftmp = explode(".",$file);
$fExt = strtolower($ftmp[count($ftmp)-1]);
if(!in_array($fExt,$extensions)){
die("ERROR: File extension not recognized: $fExt");
}
// if it was ok, let's download it
header("Content-Transfer-Encoding: binary");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=$file");
header("Content-Length: $size");
$fp=fopen("$file", "r");
fpassthru($fp);
Código PHP:
<?php
if (!isset($_GET['file']) || empty($_GET['file'])) {
exit();
}
$root = "archivos/";
$file = basename($_GET['file']);
$path = $root.$file;
$type = '';
if (is_file($path)) {
$size = filesize($path);
if (function_exists('mime_content_type')) {
$type = mime_content_type($path);
} else if (function_exists('finfo_file')) {
$info = finfo_open(FILEINFO_MIME);
$type = finfo_file($info, $path);
finfo_close($info);
}
if ($type == '') {
$type = "application/force-download";
}
// Definir headers
header("Content-Type: $type");
header("Content-Disposition: attachment; filename=$file");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . $size);
// Descargar archivo
readfile($path);
} else {
die("El archivo no existe.");
}
?>
Código PHP:
header('Content-Type:application/octet-stream'); // rar
header('Content-Type:text/plain'); // txt, html, etc
header('Content-Type:application/force-download');
header('Content-Description:File Transfer');
header('Pragma:public');
header('Expires:0');
header('Cache-Control:no-cache,must-revalidate,post-check=0,pre-check=0');
header('Cache-Control:private,false');
header("Content-Disposition:attachment;filename=$file");
header('Content-Length:'.filesize( $file ));
Gracias