-
download di un file
salve premetto che sono quasi del tutto ignorante sulla programmazione php
mi sono imbattuto in un piccolo problema mentre installavo alcuni script.
quando clikko sul link di download si apre regolarmente il download ma mi scarica un file con o kb con il nome che io imposto come file da scaricare. cioè mi crea un nuovo file vuoto e lo scarica, ignorando il file da scaicae realmente. qualcuno sa spiegare il perché?
vi copio la parte del file index che dovrebbe riguardare il download
Codice PHP:
elseif ($action == "dlid") {
# Send product download to browser.
$parts = parse_url($_SERVER["HTTP_REFERER"]);
$host = str_replace("www.", "", strtolower($parts["host"]));
if ($host == strtolower($sys_domain)) {
# It's cool. They're downloading from our domain.
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: binary");
header("Content-Description: File Transfer");
if ($_GET["oto"]) {
$fparts = explode("/", $sys_oto_location);
$filename = $fparts[count($fparts)-1];
header("Content-Disposition: attachment; filename=$filename");
@readfile($sys_oto_location);
} else {
$fparts = explode("/", $sys_item_location);
$filename = $fparts[count($fparts)-1];
header("Content-Disposition: attachment; filename=$filename");
@readfile($sys_item_location);
}
exit;
} else {
# Not downloading from our domain.
echo "<html>
<head>
<title>".$lang['error_title']."</title>
</head>
<body>
<p><font color=\"red\">".$lang['download_error']."</font></p>
</body>
</html>";
exit;
}
}
-
Al massimo 0 :roll:
Comunque prova questo di codice:
Codice PHP:
<?
function forceDownload($file) {
/**
* Function forceDownload:
* download any type of file if it exists and is readable
* -------------------------------------
* @author Andrea Giammarchi
* @date 18/01/2005 [17/05/2006]
* @compatibility PHP >= 4.3.0
*/
if(file_exists($file) && is_readable($file)) {
$filename = basename($file);
if(strpos(strtoupper($_SERVER['HTTP_USER_AGENT']), 'MSIE') !== false && strpos($filename, '.') !== false) {
$parsename = explode('.', $filename);
$last = count($parsename) - 1;
$filename = implode('%2E', array_slice($parsename, 0, $last));
$filename .= '.'.$parsename[$last];
}
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Content-Length:'.filesize($file));
header('Content-Transfer-Encoding: binary');
if(@$file = fopen($file, "rb")) {
while(!feof($file))
echo fread($file, 8192);
fclose($file);
}
exit(0);
}
}
$db = "TUOFILE.ESTENSIONE";
forceDownload( $db );
?>
Il problema è che in questo modo il download si avvia appena si visualizza la pagina, ma credo che se lo gestisci con le if vada bene