我正在用PHP创建一个文件,然后将其保存为
$job->fullFileName = realpath(".")."/abc.txt";
如果我在CURL中使用此路径(发送包含其他服务的电子邮件),那么它可以正常工作。我收到的电子邮件有预期的附件。很明显文件存在,CURL可以读取它。
$fileContent = '';
if (function_exists('curl_file_create')) {
// php 5.6+
$fileContent = curl_file_create($job->fullFileName);
} else {
$fileContent = '@' . realpath($job->fullFileName);
}
$params = array(
....
'files['.$job->fileName.'.epub'.']' => $fileContent
);
print_r($params);
$request = $url.'api/mail.send.json';
// Generate curl request
$session = curl_init($request);
// Tell curl to use HTTP POST
curl_setopt ($session, CURLOPT_POST, true);
// Tell curl that this is the body of the POST
curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
// Tell curl not to return headers, but do return the response
curl_setopt($session, CURLOPT_HEADER, false);
// Tell PHP not to use SSLv3 (instead opting for TLS)
curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// obtain response
$response = curl_exec($session);
curl_close($session);
但是当我尝试允许用户直接下载时,PHP没有找到它:
$app->get('/download', function (Request $request) use ($app) {
$params = $request->query;
$id = (int)$params->get('id', '');
if (!$id) {
return new Response("Job not found", 404);
}
$job = Job::fromId($app, $id);
if (!$job->fullFileName) {
return new Response("File not found", 404);
}
if (!file_exists($job->fullFileName)) {
return new Response("PHP give up", 404); //<<<------ IT RETURN THIS LINE!!!
}
$app['monolog']->error("[api.download] attempt to download $job->fullFileName");
// Without the check above, it will throw File Not found exception right here
$response = new BinaryFileResponse($job->fullFileName);
$response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT);
return $response;
});
我错过了什么?