PHP cURL上传文件报错:type缺失/无效、文件缺失 求助
PHP cURL上传文件报错:"type is missing, type does not have a valid value, file is missing"
原始代码
<?php if(isset($_POST['submit'])){ if(isset($_FILES['file']['name'])){ $cfile = curl_file_create($_FILES['file']['tmp_name'],$_FILES['file']['type'],$_FILES['file']['name']); $postRequest = array( 'file' => $cfile, 'caption' => 'My Test Caption' ); } } $curl = curl_init(); $url = 'myAPIURL'; $token = 'mySecretToken'; curl_setopt_array($curl, array( CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => http_build_query($postRequest), CURLOPT_HTTPHEADER => array( 'Authorization: Bearer '.$token, 'content-type: application/x-www-form-urlencoded' ), )); $response = curl_exec($curl); curl_close($curl); ?> <form method="post" action="" enctype="multipart/form-data"> <input type="file" name="file"> <br> <input type="submit" name="submit" value="Upload file"> </form>
API文档说明
REQUEST BODY SCHEMA: application/x-www-form-urlencoded
file(required) :string [File to upload]
caption(string) : The caption
错误信息
"type is missing, type does not have a valid value, file is missing"
问题分析
你的代码存在两个核心问题:
- 请求格式不匹配:你用
curl_file_create()构造文件对象,这是用于multipart/form-data类型的文件上传,但同时设置了content-type: application/x-www-form-urlencoded,并且用http_build_query()处理POST数据——这会把文件对象转换成无效的字符串,导致API无法识别文件内容。 - 变量未初始化风险:
$postRequest仅在表单提交时定义,页面首次加载时执行http_build_query($postRequest)会直接报错。
另外需要注意:application/x-www-form-urlencoded格式通常不用于直接传递文件,API文档中描述的file可能指文件内容的Base64编码字符串,而非原始文件对象。如果API确实要求该格式,需要将文件内容转成Base64后传递;如果是文档描述错误,实际需要multipart/form-data,则需调整cURL配置。
解决方案
方案一:按照application/x-www-form-urlencoded格式传递Base64编码的文件
将文件内容读取并编码为Base64字符串,作为file字段的值提交:
<?php $response = ''; if(isset($_POST['submit']) && isset($_FILES['file']['tmp_name']) && $_FILES['file']['error'] === UPLOAD_ERR_OK){ // 读取文件内容并转成Base64 $fileContent = file_get_contents($_FILES['file']['tmp_name']); $base64File = base64_encode($fileContent); $postRequest = array( 'file' => $base64File, 'caption' => 'My Test Caption' ); $curl = curl_init(); $url = 'myAPIURL'; $token = 'mySecretToken'; curl_setopt_array($curl, array( CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => http_build_query($postRequest), CURLOPT_HTTPHEADER => array( 'Authorization: Bearer '.$token, 'content-type: application/x-www-form-urlencoded' ), )); $response = curl_exec($curl); curl_close($curl); } ?> <form method="post" action="" enctype="multipart/form-data"> <input type="file" name="file"> <br> <input type="submit" name="submit" value="Upload file"> </form> <?php if($response): ?> <pre><?php echo $response; ?></pre> <?php endif; ?>
方案二:按multipart/form-data格式上传(如果API文档描述有误)
如果API实际接受multipart/form-data的文件上传,调整cURL配置,移除content-type头(cURL会自动生成正确的头),直接传递包含CURLFile的数组:
<?php $response = ''; if(isset($_POST['submit']) && isset($_FILES['file']['tmp_name']) && $_FILES['file']['error'] === UPLOAD_ERR_OK){ $cfile = curl_file_create( $_FILES['file']['tmp_name'], $_FILES['file']['type'], $_FILES['file']['name'] ); $postRequest = array( 'file' => $cfile, 'caption' => 'My Test Caption' ); $curl = curl_init(); $url = 'myAPIURL'; $token = 'mySecretToken'; curl_setopt_array($curl, array( CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_POST => true, // 用POST代替CUSTOMREQUEST CURLOPT_POSTFIELDS => $postRequest, // 直接传递数组,cURL自动处理multipart CURLOPT_HTTPHEADER => array( 'Authorization: Bearer '.$token // 移除content-type头,cURL会自动生成正确的multipart/form-data头 ), )); $response = curl_exec($curl); curl_close($curl); } ?> <form method="post" action="" enctype="multipart/form-data"> <input type="file" name="file"> <br> <input type="submit" name="submit" value="Upload file"> </form> <?php if($response): ?> <pre><?php echo $response; ?></pre> <?php endif; ?>
额外建议
- 始终检查
$_FILES['file']['error'],确保文件上传没有错误(比如文件过大、上传失败等)。 - 如果方案一无效,优先联系API提供者确认
file字段的具体要求,是Base64字符串还是支持multipart/form-data上传。
内容的提问来源于stack exchange,提问作者Mansur Ahamed
相关产品推荐
相关产品推荐

