PDA

View Full Version : PHP File Download stops Browsing



LooInSpain
October 22nd, 2009, 04:07 PM
I've written a download script to act as a proxy for files on my website so people can't get a direct link. The script works perfect, except that when you try to continue browsing after the file download has started, you can't!

A direct link to the file causes no such problem, you can continue to browse through the site, but when downloading through the download.php file, you can't - it just times out.

Here's my script:


<?php
# Declare function
function send_download($filename,$phone_model){
$file_path = "software/$phone_model/$filename";
$filetype = strrchr($filename,'.');
$ext = strtolower($filetype);
$ext = str_replace(".", "", $ext);
switch($ext) {
case "zip":
$ext = 'application/zip';
break;
case "exe":
$ext = 'application/octet-stream';
break;
case "dmg":
$ext = 'application/x-apple-diskimage';
break;
}

$file_size = filesize($file_path);

// I even tried to limit the d/l rate which didn't help
$download_rate = 1.5;

header("Content-Description: File Transfer");
header("Content-Type: $ext");
header("Content-disposition: attachment; filename=$filename");
header("Content-Transfer-Encoding: binary");
header("Content-Length: $file_size");
//readfile($file_path);
$file = @fopen($file_path,"rb");
if ($file) {
while(!feof($file)) {
set_time_limit(0);
print fread($file, round($download_rate * 1024));
flush();
sleep(1);
if (connection_status()!=0) {
@fclose($file);
die();
}
}
@fclose($file);
}
}
if (isset($_GET['file'])){
$file = $_GET['file'];
$member_query = mysql_query("SELECT * FROM members WHERE email_address = '$memberID'") or die (mysql_error());
while ($r = mysql_fetch_array($member_query)) {
$phone_model = $r["phone_model"];
}

if (file_exists("software/$phone_model/$file")) {
send_download($file, $phone_model);
} else {
echo "You Are Not Allowed to Download This Content";
}
} else {
echo "Error";
}
?>
This page opens in a blank window and sends the file. Can anyone see why it would slow down/stop the browsing in the parent window? Please help! This is not a browser specific bug either - tested in IE, Firefox, Chrome and Mac based Safari - all with the same results.

KontYentE
October 31st, 2009, 05:34 AM
Hello Looinspain...

Well i was just searching for someone with the same problem as me...

If you try to just

readfile($file_path);

Will this do the same???

It seems you'r under a session as user or not user, so the browser doesn't know what to do until the looping stops..

while(!feof($file)) {
set_time_limit(0);
print fread($file, round($download_rate * 1024));
flush();
sleep(1);

This is normal...

Some how you have to start a new session before looping otherwise you won't find any solution for you... I'm searching for another solution as well.. in my case i realy don't want to start another session...

If you find something... just report it please :)

KontYentE
October 31st, 2009, 06:12 AM
Just try
session_destroy();
before the script starts and you'll see that it works..


<?php
# Declare function
function send_download($filename,$phone_model){
$file_path = "software/$phone_model/$filename";
$filetype = strrchr($filename,'.');
$ext = strtolower($filetype);
$ext = str_replace(".", "", $ext);
switch($ext) {
case "zip":
$ext = 'application/zip';
break;
case "exe":
$ext = 'application/octet-stream';
break;
case "dmg":
$ext = 'application/x-apple-diskimage';
break;
}

$file_size = filesize($file_path);

// I even tried to limit the d/l rate which didn't help
$download_rate = 1.5;

session_destroy(); // ->HERE<-

header("Content-Description: File Transfer");
header("Content-Type: $ext");
header("Content-disposition: attachment; filename=$filename");
header("Content-Transfer-Encoding: binary");
header("Content-Length: $file_size");
//readfile($file_path);
$file = @fopen($file_path,"rb");
if ($file) {
while(!feof($file)) {
set_time_limit(0);
print fread($file, round($download_rate * 1024));
flush();
sleep(1);
if (connection_status()!=0) {
@fclose($file);
die();
}
}
@fclose($file);
}
}
if (isset($_GET['file'])){
$file = $_GET['file'];
$member_query = mysql_query("SELECT * FROM members WHERE email_address = '$memberID'") or die (mysql_error());
while ($r = mysql_fetch_array($member_query)) {
$phone_model = $r["phone_model"];
}

if (file_exists("software/$phone_model/$file")) {
send_download($file, $phone_model);
} else {
echo "You Are Not Allowed to Download This Content";
}
} else {
echo "Error";
}
?>

This way it will work for shure but then you have to start your session again some how...

F_Tanori
November 4th, 2009, 05:32 PM
Hi,

First, Sorry My English (I'am understand slow (very slow :())

tries to remove the headers

http://www.php.net/manual/en/function.header-remove.php

or try also to open the download in a popup

regards, FTanori
================================================== ==============

Intenta Remover las cabeceras del archivo, quizas eso no te permita la navegacion, o prueba tambien, disparar la descarga en un popup

Saludos
================================================== ==============