【发布时间】:2023-10-03 17:49:01
【问题描述】:
我在 Paw(和/或 Postman)中创建了一个端点来处理文件上传。效果很好!
这很简单。在我的应用程序中,我可以执行以下操作:
echo file_get_contents('php://input');
它会打印出图像/文件的编码表示(一只可爱的小猫)。
问题是,我似乎无法仅使用 cURL 重现此行为(当前应用程序使用 Guzzle)。
当我尝试类似:
$target_url = 'http://my-document-handling-service.net:8000/documents';
$request = curl_init($target_url);
curl_setopt($request, CURLOPT_POST, true);
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
curl_setopt($request, CURLOPT_POSTREDIR, 3);
curl_setopt(
$request,
CURLOPT_POSTFIELDS,
array(
'file' => '/Users/xxxxxxx-xxxx/Documents/kitten-test-upload-image-1.jpg'
));
echo curl_exec($request);
curl_close($request);
正文 (file_get_contents()) 始终为空。我不太了解 cURL 我一直只使用 Guzzle - 但现在我必须使用 cURL 并且无法获取正文。
我做错了什么?
【问题讨论】:
-
也许可以尝试将其更改为
echo file_get_contents($_POST['file']);.... 尽管您可能应该在将其放入file_get_contents之前进行一些过滤和白名单并检查该值...只是说。那是可怕的安全风险。 -
然后我得到
Warning: file_get_contents(/Users/mike/code/local-dev/kitten.jpg): failed to open stream: No such file or directory in ... (my docker filepath)。所以它就像它不发送文件 - 它只是发送文件的名称? -
对,您正在向文件发送“路径”。所以我假设
my-document-handling-service.net上不存在该路径。这反过来又抛出了这个错误。所以你正试图通过 curl 发送 ACTUAL 文件? -
在路径前添加
'@' ....'file' => '@'.'/Users/xxxxxxx-xxxx/Documents/kitten-test-upload-image-1.jpg' -
使用php curl上传文件:*.com/a/15200804/2960971 ... code.iamkate.com/php/sending-files-using-curl