开发者

sudden database disconnection in following code

I am recording the time at which the user downloaded a specific file using the following code. However, in this code, initially the download time is coming but later it's disconnecting the data base connection between client and server. If I remove the 'exit' (as shown), everything is coming fine but the downloaded file can be corrupted or damaged.

Can anyone c开发者_StackOverflowheck this code and explain what is wrong with it? I think the problem is with the exit, but what can I use instead of exit?

<?php

$f_name = $_POST["fn"];

$file = "../mt/sites/default/files/ourfiles/$f_name";

if (file_exists($file)) {
   header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
   header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
  //ob_clean();
  // flush();
    readfile($file);
  //  exit;
}
$con = mysql_connect("localhost","mt","mt");
if (!$con) {
  die('Could not connect: ' . mysql_error());
} else {
  echo "Connected";
}

// Create table
mysql_select_db("mt", $con);

mysql_query("INSERT INTO down_time (FileName,DateTime)
VALUES ('".$f_name."',NOW())");
mysql_close($con);

?>


If that happens on large files and/or slow connections, try to tweak max_execution_time in php.ini or from script using ini_set function.


well if you include the exit, your code just doesn't come to the point where it should insert the filename into db.

if you don't include the exit, you send the file contents and append "Connected" to it so the file has to be corrupted.

maybe you can try ob_start and ob_end_clean around your db stuff: http://php.net/manual/en/function.ob-start.php

this prevents anything from being sent to the output, so you don't have to use exit but nothing gets sent to the output after your file so it doesn't get corrupted

something like:

    readfile($file);
}
ob_start();
$con = mysql_connect("localhost","mt","mt");
//all the DB stuff
mysql_close($con);
ob_end_clean();
?>

you can include exit after the ob_end_clean() just to be sure but this should work just fine.


Try:

<?php
$f_name = $_POST["fn"];
$file = "../mt/sites/default/files/ourfiles/$f_name";

if (!file_exists($file)) { die('File not found'); }

if (!$con = mysql_connect("localhost","mt","mt")) { die(mysql_error()); }
if (!mysql_select_db("mt")) { die(mysql_error()); }
$q = "INSERT INTO `down_time` (`FileName`, `DateTime`) VALUES ('"
   . mysql_real_escape_string($f_name) . "',NOW())";
if (!mysql_query($q)) { die(mysql_error()); }

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
readfile($file);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜