簡體   English   中英

PHP,file_get_contents() 為 PDF 文件返回 null

[英]PHP, file_get_contents() returning null for PDF files

我想將 pdf 文件的內容放入一個變量中。 我嘗試使用

file_get_contents("file.pdf")

但它返回NULL。 例如,如果我對 .jpg 文件做同樣的事情,它就可以工作。 知道如何克服這個問題嗎? 還有其他方法嗎? 提前致謝!

編輯:

服務代碼:

<?php

function sendAttachment($msg){
$responsePayloadString = <<<XML
    <payload:receiveReport xmlns:payload="http://wso2.org/wsfphp/samples/mtom">
        <payload:reports> 
            <payload:report>
                <payload:content xmlmime:contentType="application/pdf" xmlns:xmlmime="http://www.w3.org/2004/06/xmlmime">
                    <xop:Include xmlns:xop="http://www.w3.org/2004/08/xop/include" href="cid:report1"></xop:Include>
                </payload:content>
            </payload:report>
        </payload:reports>
    </payload:receiveReport>
XML;
$report1 = file_get_contents("samplePDF.pdf");

$responseMessage = new WSMessage($responsePayloadString, 
        array("attachments" => array("report1" => $report1),
                "useWSA" => TRUE));  
return $responseMessage;    
}

$operations = array("receiveReport" => "sendAttachment");

$service = new WSService(array("operations" => $operations, 
                                            "requestXOP" => TRUE, 
                                            "useMTOM" => TRUE));

$service->reply();

?>

客戶代碼:

<?php
error_reporting(E_ALL); 
ini_set('display_errors', 1);

/******* FirePHP Debug *******/
require_once('../FirePHPCore/FirePHP.class.php');
ob_start();
$firephp = FirePHP::getInstance(true);
/******* FirePHP Debug *******/

$requestPayloadString = <<<XML
<receiveReport></receiveReport>

XML;

try {
$client = new WSClient(
    array( "to" => "http://localhost/ReportDL/ReportService.php",
           "useMTOM" => TRUE,
           "responseXOP" => TRUE,
           "useWSA" => TRUE));

$requestMessage = new WSMessage($requestPayloadString);                    
$responseMessage = $client->request($requestMessage);

$cid2stringMap = $responseMessage->attachments;
$cid2contentMap = $responseMessage->cid2contentType;

if($cid2stringMap && $cid2contentMap){
    foreach($cid2stringMap as $i=>$value){
        $f = $cid2stringMap[$i];
        $contentType = $cid2contentMap[$i];

        $firephp->log($f, "pdf");       //DEBUG

    }
}else{
    printf("attachments not received ");
}

} catch (Exception $e) {

if ($e instanceof WSFault) {
    printf("Soap Fault: %s\n", $e->Reason);
} else {
    printf("Message = %s\n",$e->getMessage());
}
}

?>

編輯2:

string '%PDF-1.5
%âãÏÓ
2 0 obj
<</Length 285/Filter/FlateDecode>>stream
xœ’_KÃ0Åßó)Ä{“4i|S¨‚ÓÀžµKiçÚ­0¿½i;ëýƒ#ÐÐÃýœž¦dwŽI
&ÒàV,qlÁJfŒ�%hCåÙ’­;YǪÕrÿÎÿêeã|}O@\Æ0,‹<ÌXäE¯6OW°‹‡z
ñÑ
Z¸}¼t]®æ$ä’Õð³ToGÞ!5¾í»R ›4Ù<)¤:•&ž@©ù¸v’4®ƒžB®gÁ6è49X»P‚c@uÌíPñîÝÃҿ“ß|V;Hö/Å&÷Ðw?f    I.MHq²Mö>­w~5k$‘8Šq£ç:õÛmVçù?òi©ý'Ó-Í^$eNðÿ\¥sá6ø(¼`ßè¿Á÷
endstream
endobj
6 0 obj
<</Type/Catalog/PageMode/UseNone/Pages 3 0 R>>
endobj
7 0 obj
<</Creator(BIRT Report Engine 2.6.0 using iText version unknown.)/Producer('... (length=1231)

如果文件不可讀, file_get_contents()應該返回FALSE - 否則它返回一個包含文件內容的字符串。

問題一定出在其他地方 - 使用var_dump()以程序方式開始調試代碼,以找出數據丟失的位置。

您需要在 PHP 頁面的頂部添加以下代碼。

header("Content-type:application/pdf");

如果其他人為此而來,我的問題是文件權限對於 www-data 不正確。 更改權限后,Web 服務器可以讀取它並按預期運行。

我知道這是一件“廢話”的事情,但起初對我來說並不明顯。

if ($file_ext == 'pdf') { $content_type = "application/pdf"; }
    elseif($file_ext == 'ppt') { $content_type = "application/vnd.ms-powerpoint"; }
    elseif($file_ext == 'pptx') { $content_type = "application/vnd.openxmlformats-officedocument.presentationml.presentation"; }
    elseif($file_ext == 'xls') { $content_type = "application/vnd.ms-excel"; }
    elseif($file_ext == 'xlsx') { $content_type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; }
    elseif($file_ext == 'doc') { $content_type = "application/vnd.ms-word"; }
    elseif($file_ext == 'docx') { $content_type = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; }
        else { $content_type = "application/octet-stream"; }

$document_contents = file_get_contents("/Server/location/to/$file");

header("Content-type: $content_type");
echo $document_contents;
die();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM