使用PHP将图像上传到维基媒体


upload image to wikimedia with php

我使用API登录到维基媒体服务器,但无法上传我收到此错误:谢谢我得到了这个错误,

文件上传

参数文件不是文件上传;请确保对 POST 使用多部分/表单数据,并在内容处置标头中包含文件名

这是图片上传的代码:

$postdata = "action=upload&format=php&filename=Image.jpg&file=@".realpath("Image.jpg")."&token=" . urlencode($_SESSION["csrftoken"]);
$app["curloptionscostun"] = array(
    CURLOPT_COOKIEFILE =>$CookieFile,
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_USERAGENT => $app["useragent"],
    CURLOPT_POST => true,
    CURLOPT_HEADER => true,
    CURLOPT_HTTPHEADER => "Content-Type:multipart/form-data"
);
$ch = curl_init();
curl_setopt_array($ch, $app["curloptionscostun"]);
curl_setopt($ch, CURLOPT_URL, $app["apiURL"]);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
$result = unserialize(curl_exec($ch));
if(curl_errno($ch)){
    echo "Error 004: " . curl_error($ch);
}
curl_close($ch);

这样更好吗?

curl_setopt(
    $ch,
    CURLOPT_POSTFIELDS,
    array(
        'action' =>'upload',
        'format'=>'php',
        'filename'=>'Image.jpg',
        'token' => urlencode($_SESSION["csrftoken"]),
        'file' => '@' . realpath('Image.jpg')
));