dougong2306 2016-09-12 06:02
浏览 389

PHP文件下载代码运行但在我的localhost上没有输出任何内容

foreach ($idsonly as $key=>$price) {  

    if($curencyarray[$key]=="Internet Download"){

        $dfr = $productidarray[$key].'.'.$itemformatarray[$key];
        $file = 'C:wamp/www/allcars/Dhotois/'.$bigidarray[$key].'/'.$dfr;
        $fie = str_replace("; charset=binary","",$mimeformarray[$key]);
        $filename = $dfr;
        $filepath = "C:wamp/www/allcars/Dhotois/".$bigidarray[$key]."/" . $filename;

        set_time_limit(0);
        ignore_user_abort(false);

        ini_set('output_buffering', 0);
        ini_set('zlib.output_compression', 0);

        header("Content-Description: File Transfer");
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Cache-Control: private",false);
        header("Content-Type: ".$fie."");
        header("Content-Disposition: attachment; filename=\"".basename($filepath)."");
        header("Content-Transfer-Encoding: binary");
        header("Content-Length: ".@filesize($filepath));

        set_time_limit(0);
        @readfile("$filepath") or die("File not found.");
        ob_clean();
    }
}

The above block of code runs but does not display the download box, the file i am trying to download is a jpeg file, i already set ob_start(); at the top of the page and if i remove the ob_clean(); from the code, it shows me this gibberish below

0041430000c945000015480000887a0000b3ca000094cf0000e1d6000011de0000a9860100��ICC_PROFILElcmsmntrRGB XYZ �)9acspAPPL���-lcms desc�^cprt\wtpthbkpt|rXYZ�gXYZ�bXYZ�rTRC�@gTRC�@bTRC�@descc2textFBXYZ ���-XYZ 3�XYZ o�8��XYZ b����XYZ $����curv��c�k�?Q4!�)�2;�FQw]�kpz���|�i�}���0����C "##! %*5-%'2( .?/279<<l� \8k�bR�K}Q�:�Câ��8�!�(��I5��6�}�xd�{���v���G���}�'��f<���c��������>��~m}|�l>��� ��Ǎ4�]�l����ϕ��Ǘ���3��1�ُ��b_�π�}��2=��B��G�m��^�x�7,+9m[�F�EW0i-�6- �"��<���"H��S��I�Db�{������)���|����BIL�d�������L&� ����a_��A��!�o�y{Iw���*T�Md�E����I�x�Q����=sv34��{���d�Y*8_-潏��K �ǭ���\ͪ2f:�%sp*�܂�#�҉tďu5���' �t!�\�f�}ֻ�9��ak�2���(� �8���qs�޻�5��P�y�3e2P��7��W5��s�����Dߐn�4��,d[cBk�,�����&o>�2��>o?���S�I�X�ڭ�T�� �@G��K�>\� d�wL�&v'����Zty��b����i�8���Plgj��J���='���j����Y�n�帨��@�ի�\Z�����K��}w�mF3��?+�nMf0���T��-�45�m���,�-��JV$ad �!�(�u� �8��!Q��>"{���������W>�3� �5Rӣe}��H��S9�sw �Puf�Ud�5�2���*�9f$anys)D���x?{U<ߣ��d.�g�F7��4���˲�Uf�\��ͧq�lѱ%����&��F'Hp�\�#�J�08Ā+�H@�9w6n�]��k������8ک�_Y�~�E�%��۸�{p��]F�2,Y�ػ�|x�[�(��g

please, what could possibly be wrong with my code?, any help and i will be so grateful.

  • 写回答

1条回答 默认 最新

  • dsuikgi9199 2016-09-12 06:18
    关注

    The path C:wamp/www looks peculiar, should there not be a slash after the colon? Anyway, when sending files I have always used as below so perhaps it will be useful.

    foreach ($idsonly as $key=>$price) {  
    
        if($curencyarray[$key]=="Internet Download"){
    
            $dfr = $productidarray[$key].'.'.$itemformatarray[$key];
            $file = 'C:/wamp/www/allcars/Dhotois/'.$bigidarray[$key].'/'.$dfr;
            $fie = str_replace("; charset=binary","",$mimeformarray[$key]);
            $filename = $dfr;
            $filepath = "C:/wamp/www/allcars/Dhotois/".$bigidarray[$key]."/" . $filename;
    
            if( realpath( $filepath ) ){
    
                set_time_limit(0);
                ignore_user_abort(false);
    
                ini_set('output_buffering', 0);
                ini_set('zlib.output_compression', 0);
    
                header("Content-Description: File Transfer");
                header("Pragma: public");
                header("Expires: 0");
                header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
                header("Cache-Control: private",false);
                header("Content-Type: ".$fie."");
                header("Content-Disposition: attachment; filename=".basename($filepath));
                header("Content-Transfer-Encoding: binary");
                header("Content-Length: ".@filesize($filepath));
    
                set_time_limit(0);
    
                /* send the file piece by piece */
                if( $file = fopen( $filepath, 'rb' ) ) {
                    while( !feof( $file ) and ( connection_status()==0 ) ) {
                        print( fread( $file, 1024*8 ) );
                        flush();
                    }
                    fclose( $file );
                }
                ob_clean();
            }
        }
    }
    

    I have a function especially for sending files, it's basic but works and can be extended to include the other headers you set above.

    function sendfile( $filename, $filepath ) {
        if( file_exists( $filepath ) ) {
    
            if( !is_file( $filepath ) 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( $filepath ) ) );
            header("Content-Disposition: inline; filename={$filename}");
            header("Content-Transfer-Encoding: binary
    ");
    
    
            if( $file = fopen( $filepath, 'rb' ) ) {
                while( !feof( $file ) and ( connection_status()==0 ) ) {
                    print( fread( $file, 1024*8 ) );
                    flush();
                }
                fclose( $file );
            }
    
            clearstatcache();
            return( ( connection_status()==0 ) and !connection_aborted() );
        }
    }
    
    
    $result=sendfile('mypicture.jpg','c:/wwwroot/images/banana.jpg');
    
    评论

报告相同问题?