说明

使用phpstudy,极少会出现这种情况。
这里主要是帮助大家理解,为什么上传的木马不运行。

问题原因

首先需要理解,访问PHP文件弹出下载,说明服务端的容器(比如Apache或者Nginx)把文件当成了一个普通二进制文件对待,例如.mp3、.pdf,这个是完全由服务端决定的。

通常来说,这类文件响应给浏览器的响应头里面会出现Content-Disposition

<?php
$file = 'example.txt'; // 要下载的文件路径

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="' . basename($file) . '"');
    header('Content-Length: ' . filesize($file));
    readfile($file);
    exit;
} else {
    echo '文件不存在.';
}
?>

Apache配置

对于服务器的配置Apache\httpd.conf来说主要是这么几个地方:

1、模块文件让apache可以解析php
LoadModule php7_module F:/php-7.3.6/php7apache2_4.dll

2、php配置文件的路径
PHPIniDir F:/php-7.3.6

3、增加php解析文件的类型
AddType application/x-http-php .php

4、解决一直下载问题

</FilesMatch>
<FilesMatch "\.phps$">
SetHandler application/x-httpd-php-source
</FilesMatch>

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐