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

我的download.php脚本发送了一个损坏的文件,php,file,download,Php,File,Download,我在编写download.php脚本时遇到困难。客户从我的数据库中获得一个唯一的下载链接,当他们单击该链接时,它意味着从我的服务器向他们发送文件。只有在我的情况下,zip文件总是损坏的,并且比原始文件大。原始文件为68KB,下载的文件最终为90KB 以下是我将文件发送到浏览器的代码: <?php $actual_link = "http://$_SERVER[HTTP_HOST]"; $post_id = $wpdb->get_results("SELECT transaction

我在编写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;
}

首先,您不能执行echo谢谢…,因为它被附加到ZIP文件中。只需删除这一行。然而,这还不能解释文件中有多少额外的KB。还有一个问题。我将删除echo命令,并添加一个Content lenght头。您正在向readfile传递一个URL,例如。http://.... . 这是你的意图吗?乍一看,这似乎不对,因为在这种情况下,readfile通常用于从服务器的文件系统而不是从网络URL读取。您似乎混淆了两种不同的方法,要么生成URL并将用户转发到我认为更容易的位置,要么从本地磁盘读取文件,不是URL。@JamieKitson所以我应该更改这一行$file\u URL=$actual\u link./jomsplugins/$name;这个$文件_url=../jomsplugins/$name;