无法上传文件与curl在heroku php应用程序 [英] Failing to upload file with curl in heroku php app
问题描述
每当我尝试上传文件时,我收到来自curl的响应failed creating formpost data。我从网络上的其他帖子感觉到它的文件路径是错误的,但我似乎无法获得一个绝对路径heroku。
当我做 dirname(__ FILE __)
所有我得到的是 / app / www /
,不能是正确的吗?如何在heroku应用程序中获取绝对文件路径?
这是我的整行文件路径,包括在post数据
@dirname(__ FILE __)。/ images / wallimages /'。random_pic()
pre>
和我的卷曲设置:
$ 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(文件'$ uploadPath'不存在);
if(!is_readable($ uploadPath))die(文件'uploadPath'不可读);
$ postFields = array(
'nameOfFileControl'=>@ $ uploadPath,
'someOtherFormControl'=>'某些值'
//调整这个数组与你的post字段匹配。
//确保数组保持这种格式!
);
$ 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;
I'm getting the response "failed creating formpost data" from curl whenever I try to upload a file. I'm sensing from other posts on the web that it's the filepath that's wrong, but I can't seem to get an absolute path on heroku.
When i do
dirname(__FILE__)
all I get is/app/www/
that can't be right can it? How do I get the absolute file path in a heroku app?this is my entire line for the image path to be included in post data
"@".dirname(__FILE__).'/images/wallimages/'.random_pic()
and my curl setup:
$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;
解决方案I suspect the root cause of your problem may be this line from the manual:
As of PHP 5.2.0, value must be an array if files are passed to this option with the @ prefix.
In any case, try this code and if it doesn't work, let me know what error message(s) you get so I can debug:
$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;
这篇关于无法上传文件与curl在heroku php应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!