Php 下载代码在wordpress中不起作用

Php 下载代码在wordpress中不起作用,php,wordpress,download,Php,Wordpress,Download,当我下载图像时,它会在屏幕上显示这些字符 �M\cIDATx��\i��v�U�ݳ��x���6`8�Y�&@��#D �����HDʟ'�~�z;a���D,�c������ƌ���ٺ�*�w�=շjz^z�ـ���UwWWݺ�;�|g��&H���o�Gy�The content type is wrong application/octet-stream �M\cIDATx��\我��v�U�ݳ��x���6`8�Y�&@��#D�����HDʟ�~�ZA.���D�C

当我下载图像时,它会在屏幕上显示这些字符

�M\cIDATx��\i��v�U�ݳ��x���6`8�Y�&@��#D �����HDʟ'�~�z;a���D,�c������ƌ���ٺ�*�w�=շjz^z�ـ���UwWWݺ�;�|g��&H���o�Gy�The content type is wrong

application/octet-stream

�M\cIDATx��\我��v�U�ݳ��x���6`8�Y�&@��#D�����HDʟ�~�ZA.���D�C������ƌ���ٺ�*�W�=շjz^z�ـ���UwWWݺ�;�|G��&H���o�Gy� 内容类型错误

image/jpeg
Ocet流用于图像不确定的可执行文件

jpg图像的适当图像类型如下:

$fn = $path.'/'.$file_name;
$mime = mime_content_type($fn);
header('Content-Type:'.$mime);
header('Content-Length: ' . filesize($fn));
readfile($fn);
您可以使用获取文件的适当内容类型

返回通过使用magic.MIME文件中的信息确定的文件的MIME内容类型

试试这个代码

<?php
    $fn = $path.'/'.$file_name;
    $mm_type="application/octet-stream";
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header("Content-Length: " .(string)(filesize($fn)) );
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Disposition: attachment; filename="'.$fn.'"');
    header("Content-Transfer-Encoding: binary\n");
    ob_clean();
    flush();
    readfile($fn);
    exit;
?>

内容类型错误

image/jpeg
Ocet流用于图像不确定的可执行文件

jpg图像的适当图像类型如下:

$fn = $path.'/'.$file_name;
$mime = mime_content_type($fn);
header('Content-Type:'.$mime);
header('Content-Length: ' . filesize($fn));
readfile($fn);
您可以使用获取文件的适当内容类型

返回通过使用magic.MIME文件中的信息确定的文件的MIME内容类型

试试这个代码

<?php
    $fn = $path.'/'.$file_name;
    $mm_type="application/octet-stream";
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header("Content-Length: " .(string)(filesize($fn)) );
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Disposition: attachment; filename="'.$fn.'"');
    header("Content-Transfer-Encoding: binary\n");
    ob_clean();
    flush();
    readfile($fn);
    exit;
?>
试试这个

# my demo value in my local machine
$path = dirname(__FILE__) . "/demo";
$file_name = "Capture.PNG";    
#$fn = realpath($path.'/'.$file_name);
$fn = "http://thetexturemill.com/wp-content/uploads/2013/07/dell.png";
//var_dump(readfile($fn));

$mm_type="application/octet-stream";
#$mm_type=mime_content_type($fn);
#echo $mm_type; die();
ob_get_flush();
header("Cache-Control: public, must-revalidate");
header("Pragma: hack");
header("Content-Type: " . $mm_type);
#header("Content-Length: " .(string)(filesize($fn)) );
header('Content-Disposition: attachment; filename="'.$file_name.'"');
header("Content-Transfer-Encoding: binary\n");
readfile($fn);
flush();
die();

读这个

试试这个

# my demo value in my local machine
$path = dirname(__FILE__) . "/demo";
$file_name = "Capture.PNG";    
#$fn = realpath($path.'/'.$file_name);
$fn = "http://thetexturemill.com/wp-content/uploads/2013/07/dell.png";
//var_dump(readfile($fn));

$mm_type="application/octet-stream";
#$mm_type=mime_content_type($fn);
#echo $mm_type; die();
ob_get_flush();
header("Cache-Control: public, must-revalidate");
header("Pragma: hack");
header("Content-Type: " . $mm_type);
#header("Content-Length: " .(string)(filesize($fn)) );
header('Content-Disposition: attachment; filename="'.$file_name.'"');
header("Content-Transfer-Encoding: binary\n");
readfile($fn);
flush();
die();


阅读此

我已尝试使用您的代码,但未发现任何问题。在阅读您的评论并尝试使用您的文件
thetexturemill.com/wp content/uploads/2013/07/dell.png
后,我的代码正常工作:

我发现的问题是:

  • 如果您使用来自远程主机的图像,请确保您可以获取它(
    allow\u url\u fopen
    INI选项为
    ON
    ,并且
    readfile
    返回的值大于零),并且不要使用
    filesize
    以及
    mime\u content\u type
    函数
  • 我不知道
    thetexturemill.com
    是你的域名还是文件夹名。假设它是一个域名,请记住添加协议前缀(
    http://
    ,如示例所示)
  • 头函数调用之前,不要输出任何内容,否则下载的文件将无法正常打开

啊,对于本地文件,您的原始代码在我的机器上正常工作。

我尝试了您的代码,但没有发现任何问题。在阅读您的评论并尝试使用您的文件
thetexturemill.com/wp content/uploads/2013/07/dell.png
后,我的代码正常工作:

我发现的问题是:

  • 如果您使用来自远程主机的图像,请确保您可以获取它(
    allow\u url\u fopen
    INI选项为
    ON
    ,并且
    readfile
    返回的值大于零),并且不要使用
    filesize
    以及
    mime\u content\u type
    函数
  • 我不知道
    thetexturemill.com
    是你的域名还是文件夹名。假设它是一个域名,请记住添加协议前缀(
    http://
    ,如示例所示)
  • 头函数调用之前,不要输出任何内容,否则下载的文件将无法正常打开


啊,对于本地文件,您的原始代码在我的机器上运行时没有错误。

我也尝试过,但它不运行header('Content-type:image/png');标题('Content-disposition:attachment;filename=.'dell.png.';');标题('Content-Length:'.filesize($fn1));readfile($1);你确定它是正确的图像文件吗?@Hamender检查我编辑过的版本中的代码。是的@Robert,它是png图像。我也尝试过这个,但它不工作头('Content-type:image/png');标题('Content-disposition:attachment;filename=.'dell.png.';');标题('Content-Length:'.filesize($fn1));readfile($1);你确定这是正确的图像文件吗?@Hamender检查我编辑的代码版本。是@Robert,这是png imageyes@Rohan我尝试了,现在显示登录名window@Hamender再次测试上述内容。再次使用这些特殊字符打开登录窗口删除
ob_clean()行,然后测试它。我删除了它,但没有任何效果,相同的结果登录页面带有这些字符。我尝试了@Rohan,现在显示登录window@Hamender再次测试上述内容。再次使用这些特殊字符打开登录窗口删除
ob_clean()行,然后对其进行测试。我删除了它,但没有任何效果,使用这些字符的相同结果登录页面遇到问题时,我知道
$file\u name
变量的值是多少?我试过你的代码,没有发现错误。您可以添加
var\u dump($fn);die()
$mm_type=..
之前,并将输出结果告诉我。@Trung HieuLe$fn=和$file_name=dell.pngMy代码可以处理您的文件。你可以看看我的答案并告诉我结果。@Trung HieuLe给我你的答案哪一个有效proper@Hamender在下面,当您遇到问题时,我可以知道
$file\u name
变量的值是多少吗?我试过你的代码,没有发现错误。您可以添加
var\u dump($fn);die()
$mm_type=..
之前,并将输出结果告诉我。@Trung HieuLe$fn=和$file_name=dell.pngMy代码可以处理您的文件。你可以看看我的答案并告诉我结果。@Trung HieuLe给我你的答案哪一个有效proper@Hamender当我在本地主机上执行此警告时,它位于bellow@Trun HieuLe,带有相同的字符警告:无法修改标题信息-标题已由发送(输出开始于/opt/lampp/htdocs/download/downloading.php:12)第13行的/opt/lampp/htdocs/download/downloading.php警告:无法修改标题信息-标题已由第14行的/opt/lampp/htdocs/download/downloading.php:12)的/opt/lampp/htdocs/download/downloading.php发送。您能告诉我php下载文件的确切位置以及如何在wordpress中使用它吗。你在插件或主题中使用它吗?这可能会有很大帮助。@Trun HieuLe当我在localhost上执行此警告时,使用相同的字符警告:无法修改标题信息-标题已由/opt/lampp/htdocs/download/downloading.php:12)在/opt/lampp/htd中发送