You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress调用GitLab Upload File API返回'file is invalid'问题排查

GitLab上传文件API返回{"error":"file is invalid"}问题排查

我需要将WordPress后端中ContactForm7表单上传的图片存储后,上传至GitLab项目用于Issue内容。使用GitLab的上传文件API时,无论发送何种内容,始终返回{"error":"file is invalid"}。参考过相关问题的解决方案但无效,且我不想将图片提交到仓库,仅需使用GitLab的上传架构。

我尝试过以下几种传入方式,均返回相同错误:

  • @image.png
  • @/wordpress/uploads/path/to/image.png
  • @https://example.com/image.png
  • @base64编码的图片内容

找不到GitLab对图片格式要求的相关文档,请问GitLab期望的上传格式是什么?

我的PHP代码

function uploadToGitLab($contact_form) {
    $contact_form_id = $contact_form->id();
    $submission = WPCF7_Submission::get_instance();
    $data = $submission->get_posted_data();
    $files = $submission->uploaded_files();
    
    $markdownImage = "";
    if (!empty($files) && $contact_form_id == "721") {
        // 将图片从临时文件夹移动到上传文件夹
        $image_file = file_get_contents($files['anhang'][0]);
        $image_name = basename($files['anhang'][0]);
        $imageUpload = wp_upload_bits(basename($files['anhang'][0]), null, $image_file);    
        $image_filename = $imageUpload['url'];

        // 转换图片为Base64编码
        $image_type = pathinfo($imageUpload['file']);
        $image_stringdata = file_get_contents($imageUpload['file']);
        $image_base64 = base64_encode($image_stringdata);
        
        $uploadImageHeader = array(
            'Content-type' => 'multipart/form-data',
            // 尝试发送Base64图片时设置编码:
            'encoding' => 'base64'
        );
        $uploadImageBody = array(
            // 使用表单上传的原始文件:
            //'file' => '@'. $files["anhang"][0]
            // 仅使用文件名:
            //'file' => '@'. basename($files["anhang"][0])
            // 使用图片公网URL:
            //'file' => '@'. $image_filename
            // 使用图片的Base64编码内容:
            'file' => '@' . $image_base64
        );
        $uploadArgs = array(
            'header' => $uploadImageHeader,
            'body' => $uploadImageBody
        );
        $imageResponse = wp_remote_post("https://example.com/api/v4/projects/MYPROJECTID/uploads?private_token=TOKEN", $uploadArgs);
        $markdownImage = $imageResponse["markdown"];
    }
}

内容的提问来源于stack exchange,提问作者Shanksum

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 09:43:21