在PHP中下载exe文件时出现Windows兼容性错误


Windows compatibility error downloading exe files in PHP

我使用php和mysql建立了一个页面,要求用户登录以下载各种付费程序。他们可以点击这里的链接,程序就会正确下载并运行。

$c3 = mysql_result($result,$i,"exe");
echo "<a href='$c3'>... etc

然而,RT点击属性可以让他们看到该文件的路径,所以我将上面的内容改为:

$c3="downloads3.php?link=".mysql_result($result,$i,"exe");

下载3.php如下:

<?php
$file = $_GET['link'];
$size = filesize($file);
$type = filetype($file);
$path = "../downloads/";
header('Content-Type: $type'); 
header("Content-Transfer-Encoding: Binary");  
header("Content-Disposition: attachment; filename=$path.$file");
header("Content-Length: ".filesize($file)); 
readfile($file_url);?> 
?>

它找到了正确的文件,我收到了一个安全警告,但在单击"运行"时,它会立即显示一条windows错误消息,表明该文件与此版本的windows不兼容。肯定是上面的标题中有什么内容,但不知道是什么。尝试了各种排列。

有什么绝妙的想法,是让上面的东西发挥作用,还是用其他方式隐藏源路径?谢谢

EXE很可能由于意外输出而损坏。您的downloads3.php文件有一些额外的输出,将显示在下载中:

readfile($file_url);?> //PHP stops parsing here 
?> //output "'n?>"

PE头本身告诉Windows它可以在什么版本上运行,因此如果在发送文件之前生成任何错误,它们将出现在Windows期望的头的位置。

为了缓解这种情况,您可以删除文件末尾的额外换行符和?>,并使用文件顶部的error_reporting(0)关闭错误报告。

这里获得任何名称的下载文件的最佳解决方案您想要什么

function force_download($filename = '', $data = '')
{
    if ($filename == '' OR $data == '')
    {
        return FALSE;
    }
    // Try to determine if the filename includes a file extension.
    // We need it in order to set the MIME type
    if (FALSE === strpos($filename, '.'))
    {
        return FALSE;
    }
    // Grab the file extension
    $x = explode('.', $filename);
    $extension = end($x);
    // Load the mime types
    if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/mimes'.EXT))
    {
        include(APPPATH.'config/'.ENVIRONMENT.'/mimes'.EXT);
    }
    elseif (is_file(APPPATH.'config/mimes'.EXT))
    {
        include(APPPATH.'config/mimes'.EXT);
    }
    // Set a default mime if we can't find it
    if ( ! isset($mimes[$extension]))
    {
        $mime = 'application/octet-stream';
    }
    else
    {
        $mime = (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];
    }
    // Generate the server headers
    if (strpos($_SERVER['HTTP_USER_AGENT'], "MSIE") !== FALSE)
    {
        header('Content-Type: "'.$mime.'"');
        header('Content-Disposition: attachment; filename="'.$filename.'"');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header("Content-Transfer-Encoding: binary");
        header('Pragma: public');
        header("Content-Length: ".strlen($data));
    }
    else
    {
        header('Content-Type: "'.$mime.'"');
        header('Content-Disposition: attachment; filename="'.$filename.'"');
        header("Content-Transfer-Encoding: binary");
        header('Expires: 0');
        header('Pragma: no-cache');
        header("Content-Length: ".strlen($data));
    }
    exit($data);
}
$data = 'Here is some text!';
$name = 'mytext.txt';
force_download($name, $data); 

DOH!!!!!!!!!!!!!!!!剪切和粘贴太多了,那个代码有一个非常愚蠢的错误readfile($file_url)应该是readfile($file),难怪我的36Mb文件下载后只有1KB,它是空的!

谢谢你的评论,很抱歉浪费了你的时间。