gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 05:28:24 29 4
gpt4 key购买 nike

我想将 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,file_get_contents() 为 PDF 文件返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10949108/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com