如何用PHP下载解决问题?

尤金·莫斯(Eugene Mos)

伙计们。我正在使用具有laravel框架的本地服务器,并尝试下载文件。我尝试了很多案件,但对我没有任何帮助。

问题是我没有错误。

下载不会开始,但是作为回应,我有这样的东西:

�

���JFIF��;CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), quality = 95��C      
���}!1AQa"q2���#B��R��$3br�  
%&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz���������������������������������������������������������������������������    
���w!1AQaq"2�B����   #3R�br�
$4�%�&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz��������������������������������������������������������������������������?���!�A���I��+�P�sB���i��?�Y�L��iG���B���Ap�����J;���;}ic=F{R�Z��
�$��z�q��~�p8�h�v?A@��;����t�@�Fx��Ǯq�S#<������qS-

我用的是:以这种方式尝试的:

$file = 'D:\Server\OpenServer\domains\memo.ru\public\img\avatars\\'.$file_name;
$filePath=$file;

和这个:

$file = "http://example.com/img/avatars/1457091259.png";

1。

    set_time_limit(0);
    $file = @fopen($filePath, 'rb');
    while ( !feof($file) ) {
        print( @fread($file,1024*8) );
        //ob_flush();
        flush();
    }

2个

    $handle = fopen($filePath, 'rb');
    $buffer = '';
    while ( !feof($handle) ) {
        $buffer = fread($handle, 4096);
        echo $buffer;
        ob_flush();
        flush();
    }
    fclose($handle);

3

    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    //header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);

4

$real_path = 'D:\Server\OpenServer\domains\example.com\public\img\avatars\\';
$file = "http://example.com/img/avatars/1457091259.png";

$url  = 'http://example.com/img/avatars/'.$tmpFileName;
$path = $real_path.$tmpFileName;

$fp = fopen($path, 'w');

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fp);

$data = curl_exec($ch);

curl_close($ch);
fclose($fp);
约瑟夫

如果您使用的是Laravel 5,那么这将是最简单的方法:

public function download()
{

    $file = base_path(). "public/img/avatars/1457091259.png";

    $name = "Human Name For File.jpg";

    $headers = ['Content-Type: application/octet-stream'];

    return Response::download($file, $name, $headers);
}

只需说出您use Response在顶部之前的位置即可class

application/octet-stream标题应强制浏览器下载文件,而不是在浏览器窗口中显示。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章