You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
2.2 KiB
78 lines
2.2 KiB
<?php
|
|
/**
|
|
* 文件上传测试脚本
|
|
* 用于测试文档上传功能
|
|
*/
|
|
|
|
// 创建一个测试文件
|
|
$testContent = "这是一个测试文档\n创建时间: " . date('Y-m-d H:i:s');
|
|
$testFile = '/tmp/test_document.txt';
|
|
file_put_contents($testFile, $testContent);
|
|
|
|
echo "📄 文件上传测试\n";
|
|
echo "================\n";
|
|
echo "测试文件: $testFile\n";
|
|
echo "文件大小: " . filesize($testFile) . " bytes\n\n";
|
|
|
|
// 测试上传接口
|
|
$url = 'http://niucloud_nginx/api/uploadDocument';
|
|
|
|
// 创建 CURLFile 对象
|
|
$cfile = new CURLFile($testFile, 'text/plain', 'test_document.txt');
|
|
|
|
$postData = [
|
|
'file' => $cfile,
|
|
'type' => 'document'
|
|
];
|
|
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
curl_setopt($ch, CURLOPT_POST, true);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
|
'Accept: application/json',
|
|
'token: test_token_for_upload_test',
|
|
// 注意:不要设置 Content-Type,让curl自动设置为 multipart/form-data
|
|
]);
|
|
|
|
echo "🚀 发送上传请求...\n";
|
|
$response = curl_exec($ch);
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
$error = curl_error($ch);
|
|
curl_close($ch);
|
|
|
|
echo "HTTP状态码: $httpCode\n";
|
|
if ($error) {
|
|
echo "CURL错误: $error\n";
|
|
}
|
|
|
|
echo "响应内容:\n";
|
|
echo $response . "\n\n";
|
|
|
|
// 解析响应
|
|
$responseData = json_decode($response, true);
|
|
if ($responseData) {
|
|
if ($responseData['code'] == 1) {
|
|
echo "✅ 上传成功!\n";
|
|
echo "文件URL: " . $responseData['data']['url'] . "\n";
|
|
echo "文件名: " . $responseData['data']['name'] . "\n";
|
|
echo "扩展名: " . $responseData['data']['ext'] . "\n";
|
|
} else {
|
|
echo "❌ 上传失败: " . $responseData['msg'] . "\n";
|
|
}
|
|
} else {
|
|
echo "❌ 响应解析失败\n";
|
|
}
|
|
|
|
// 清理测试文件
|
|
unlink($testFile);
|
|
echo "\n🗑️ 测试文件已清理\n";
|
|
|
|
echo "\n📋 调试建议:\n";
|
|
echo "1. 检查前端是否使用了正确的参数名 'file'\n";
|
|
echo "2. 检查请求是否为 multipart/form-data 格式\n";
|
|
echo "3. 检查文件大小是否超过限制\n";
|
|
echo "4. 检查服务器错误日志\n";
|
|
echo "5. 检查腾讯云COS配置和权限\n";
|
|
?>
|
|
|