我正在使用following tutorial将图片上传到imgur。我想知道在发送数据时是否需要base64_encode
。
这是一个小代码片段:
$img=$_FILES['img'];
$filename = $img['tmp_name'];
$data = fread($handle, filesize($filename));
$pvars = array('image' => base64_encode($data)); // Here's the base64_encode
curl_setopt($curl, CURLOPT_POSTFIELDS, $pvars);
是否需要base64_encode
以及为什么?
要查看完整代码,您可以转到here。
答案 0 :(得分:1)
答案 1 :(得分:0)
不是没有必要,我不知道这是否是发布大量图片的常见做法。
您还可以使用cURL选项CURLOPT_INFILE
请注意:以下示例可能不起作用,它们只是不成功的例子。
// This is the entire file that was uploaded to a temp location.
$localFile = $_FILES[$fileKey]['tmp_name'];
$fp = fopen($localFile, 'r');
// Connecting to website.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $target_url );
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_BUFFERSIZE, 128);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localFile));
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result=curl_exec ($ch);
curl_close ($ch);
echo $result;
或只是一个简单的POST
$post = array('extra_info' => '123456','file_contents'=>'@'.$_FILES['upload']['tmp_name']);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result=curl_exec ($ch);
curl_close ($ch);
echo $result;