我的download.php脚本发送了一个损坏的文件

时间:2014-06-11 08:54:25

标签: php file download

我在编写download.php脚本时遇到了困难。客户获得从我的数据库生成的唯一下载链接,当他们点击它时,它意味着从我的服务器发送它们的文件。只有在我的情况下,zip文件总是损坏,并且比原始文件大。原始文件为68KB,下载的文件最终为90KB。

以下是我将代码发送到浏览器的代码:

<?php 
$actual_link = "http://$_SERVER[HTTP_HOST]";
$post_id = $wpdb->get_results("SELECT transaction_id FROM slx_jomsocial_plugin_orders WHERE (transaction_id ='". $tx ."' and unique_code='". $token ."')");
$rowCount = $wpdb->num_rows;
if($rowCount==1){
$name="Unzip_First_Video_Embedding.zip";
$file_url=$actual_link."/jomsplugins/".$name;   
header("Content-type: application/x-file-to-save"); 
header("Content-Disposition: attachment; filename=".$name); 
readfile($file_url);
       echo "<h1>Thank you ".$name=$_SESSION['s_name']." For downloading the plugins.</h1><br />";
}else{
       echo "<h1>You are not permitted to downlaod, Please purchase plugin first.</h1> <br /><div style=\"margin-left: 25%;margin-top: 1%;\"><a href=\"http://code-grab.com/\" title=\"Code-grab\">Click Here</a></div>";
}
?>

我在这里做错了什么?

更新:

我一直在尝试使用该脚本,但我的文件仍然损坏(解压缩它们会产生.cpgz文件)。这是我现在的脚本:

<?php 

$actual_link = "http://$_SERVER[HTTP_HOST]";
$post_id = $wpdb->get_results("SELECT transaction_id FROM slx_jomsocial_plugin_orders WHERE (transaction_id ='". $tx ."' and unique_code='". $token ."')");
$rowCount = $wpdb->num_rows;
if($rowCount==1){

// set example variables
$filename = "Unzip_First_Video_Embedding.zip";
$filepath = $actual_link."/jomsplugins/";

// http headers for zip downloads
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$filename."\"");
header("Content-Transfer-Encoding: binary");
ob_end_flush();
ob_clean();
flush();
@readfile($filepath.$filename);
ob_clean();
flush();
exit;
}

0 个答案:

没有答案