PHP文件下载代码在我的本地主机

时间:2016-09-12 06:02:54

标签: php

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();
    }
}

上面的代码块运行但没有显示下载框,我试图下载的文件是jpeg文件,我已经设置了ob_start();在页面顶部,如果我删除ob_clean();从代码中,它向我显示下面的这个乱码

0041430000c945000015480000887a0000b3ca000094cf0000e1d6000011de0000a9860100 ICC_PROFILElcmsmntrRGBXYZ )9acspAPPL -lcmsdesc ^ cprt \ wtpthbkpt |rXYZ gXYZ bXYZ rTRC @gTRC @bTRC @ descc2textFBXYZ -XYZ3 XYZo 8 XYZb 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φǗ31b_π} 2 = BGM ^ X7,+9米[ 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 ޻5Py3e2P7W5sDߐN4,d [CBK,&安培; O&GT 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)DX {U<ߣd.gF7 4˲Uf\ͧqlѱ%安培; F'Hp \#J08Ā+ H @ 9w6n] K8ک_Y〜E&8 {p] F2, ÿػ|×[(克

请问,我的代码可能有什么问题?,任何帮助,我将非常感激。

1 个答案:

答案 0 :(得分:0)

如果结肠后没有斜线,路径$scope.sryarndebitnote = [ { "_id": "57ac1b6d82e1c5940ac3c730", "user": { "_id": "57400c32bd07906c1308e2cf", "displayName": "mani selvam" }, "__v": 0, "created": "2016-08-11T06:30:05.118Z", "shipment_id": "57ac19b982e1c5940ac3c72f", "conversion_rate": "62.04", "invoice_value_fob_currency": "Rs", "invoice_value_fob": "300.231", "invoice_quantity_unit": "KG", "invoice_quantity": "37", "invoice_date": "2016-08-17", "supplier_name": "Msd", "buyer_name": "Mani selvam .R" }, { "_id": "57b5af69df0475401f644b2e", "user": { "_id": "57400c32bd07906c1308e2cf", "displayName": "mani selvam" }, "__v": 0, "created": "2016-08-18T12:51:53.671Z", "shipment_id": "57b5af5bdf0475401f644b2d", "conversion_rate": "62.06", "exclusive": true, "invoice_value_fob": "400.343", "invoice_quantity": "97", "supplier_name": "Msd", "buyer_name": "Mani selvam .R" },] 看起来很奇怪?无论如何,在发送文件时,我总是使用如下,所以它可能会有用。

C:wamp/www

我有一个特别用于发送文件的功能,它是基本但可以工作,可以扩展到包括你在上面设置的其他标题。

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();
        }
    }
}