PHP Send local file by cURL(PHP 通过 cURL 发送本地文件)
问题描述
我正在尝试通过客户端 curl 应用程序发送本地文件.我找到了一些示例来处理表单中的文件.就我而言,我没有表单,只有一个本地文件.
Im trying to send a local file by client curl app. I found some examples to do that with files from a form. In my case, I have no form, but a local file.
$fileName = $_SERVER["DOCUMENT_ROOT"]."/www/images/test.pdf";
if(!file_exists($fileName)) {
$out['status'] = 'error';
$out['message'] = 'File not found.';
exit(json_encode($out));
}
$data = array('name' => 'Foo', 'file' => '@'.$fileName);
$cURL = curl_init("http://myapi/upload-images");
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cURL, CURLOPT_POST, 1);
curl_setopt($cURL, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($cURL);
$error = curl_error($cURL);
curl_close($cURL);
die($response);
有了这个,我没有错误,但在服务器中 $_POST 和 $_SERVER 数组是空的.
With this, I have no erros, but in server the $_POST and $_SERVER arrays is empty.
我尝试了其他方法,这次在发送之前创建了一个 Curl 文件:
I tried otherwise, this time creating a Curl file before send:
// Mime type of file
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$finfo = finfo_file($finfo, $fileName);
$cFile = new CURLFile($fileName, $finfo, "file");
//var_dump($cFile);
//CURLFile Object
//(
// [name] => C:/.../test.pdf
// [mime] => application/pdf
// [postname] => file
// )
$cURL = curl_init("http://myapi/upload-images");
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cURL, CURLOPT_POST, true);
curl_setopt($cURL, CURLOPT_POSTFIELDS,
array(
'file' => $cFile
));
$response = curl_exec($cURL);
curl_close($cURL);
die($response);
同样的反应.$_FILES 为空.
Same response. $_FILES is empty.
推荐答案
终于找到问题的原因了.包含文件数据的数组必须有文件数据和文件名键.
Finally I found the reason of issue. The array with file data must have filedata and filename keys.
我们可以在带有完整路径的文件名之前传递@",但这已被弃用.
We can pass '@' before file name with full path but this is deprecated.
$data = array( "filedata" => '@'.$fileName, "filename" => basename($fileName));
在这种情况下,我添加了一个 Curl 对象:
In this case I added a Curl object:
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$finfo = finfo_file($finfo, $fileName);
$cFile = new CURLFile($fileName, $finfo, basename($fileName));
$data = array( "filedata" => $cFile, "filename" => $cFile->postname);
完整代码为:
$fileName = $_SERVER["DOCUMENT_ROOT"]."/www/images/test.pdf";
$fileSize = filesize($fileName);
if(!file_exists($fileName)) {
$out['status'] = 'error';
$out['message'] = 'File not found.';
exit(json_encode($out));
}
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$finfo = finfo_file($finfo, $fileName);
$cFile = new CURLFile($fileName, $finfo, basename($fileName));
$data = array( "filedata" => $cFile, "filename" => $cFile->postname);
$cURL = curl_init("http://myapi/upload-images")
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
// This is not mandatory, but is a good practice.
curl_setopt($cURL, CURLOPT_HTTPHEADER,
array(
'Content-Type: multipart/form-data'
)
);
curl_setopt($cURL, CURLOPT_POST, true);
curl_setopt($cURL, CURLOPT_POSTFIELDS, $data);
curl_setopt($cURL, CURLOPT_INFILESIZE, $fileSize);
$response = curl_exec($cURL);
curl_close($cURL);
die($response);
这篇关于PHP 通过 cURL 发送本地文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PHP 通过 cURL 发送本地文件
基础教程推荐
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01