每当我尝试上传文件时,我都会从curl收到"创建formpost数据失败"的响应。我从网上的其他帖子中感觉到文件路径是错误的,但我似乎无法在heroku上找到绝对的路径。
当我做dirname(__FILE__)
时,我得到的只是/app/www/
,这不可能是正确的,是吗?如何在heroku应用程序中获取绝对文件路径?
这是我的整行图像路径要包括在后数据中
"@".dirname(__FILE__).'/images/wallimages/'.random_pic()
和我的卷曲设置:
$ch = curl_init('https://url.com/'.$url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
if(curl_errno($ch))
{
return 'Curl error: ' . curl_error($ch);
}
curl_close ($ch);
return $result;
我怀疑问题的根本原因可能是手册中的这一行:
从PHP 5.2.0开始,如果文件以@前缀传递给此选项,则值必须是数组。
在任何情况下,请尝试此代码,如果它不起作用,请告诉我您收到的错误消息,以便我可以调试:
$uploadPath = rtrim(dirname(__FILE__),'/').'/images/wallimages/'.random_pic();
if (!is_file($uploadPath)) die("The file '$uploadPath' does not exist");
if (!is_readable($uploadPath)) die("The file '$uploadPath' is not readable");
$postFields = array (
'nameOfFileControl' => "@$uploadPath",
'someOtherFormControl' => 'some value'
// Adjust this array to match your post fields.
// Ensure you keep the array in this format!
);
$ch = curl_init('https://url.com/'.$url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$result = curl_exec($ch);
if(curl_errno($ch))
{
return 'Curl error: ' . curl_error($ch);
}
curl_close ($ch);
return $result;