如何用PHP下载解决问题?

时间:2016-03-05 10:16:18

标签: php laravel

人。 我正在使用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);

3 个答案:

答案 0 :(得分:0)

这应该有效

header('Content-Type: image/png');
header('Content-disposition: attachment; filename='.$fileName);
echo readfile($file);
die();

答案 1 :(得分:0)

我已经非常成功地使用以下内容来下载/发送文件 - 也许它会对你有用吗?

称之为:

call_user_func( 'send_file', 'myfile.docx', 'c:/temp/document.docx' );


function send_file( $name, $strPath ) {
    if( file_exists( $strPath ) ) { 
        if( !is_file( $strPath ) or connection_status()!=0 ) { return FALSE;    }

        header("Cache-Control: no-store, no-cache, must-revalidate");
        header("Cache-Control: post-check=0, pre-check=0", false);
        header("Pragma: no-cache");
        header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT");
        header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
        header("Content-Type: application/octet-stream");
        header("Content-Length: ".(string)( filesize( $strPath ) ) );
        header("Content-Disposition: inline; filename={$name}");
        header("Content-Transfer-Encoding: binary\n");

        if( $file = fopen( $strPath, 'rb' ) ) {
            while( !feof( $file ) and ( connection_status()==0 ) ) {
                print( fread( $file, 1024*8 ) );
                flush();
            }
            fclose( $file );
        }
        clearstatcache();
        return( ( connection_status()==0 ) and !connection_aborted() );
    }
}

答案 2 :(得分:0)

如果您正在使用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标题应强制浏览器下载文件,而不是在浏览器窗口中显示。