public function upload()
{
ini_set('upload_max_filesize', '20M');
ini_set('post_max_size', '20M');
ini_set('memory_limit', '128M');
$file = $_FILES['file'];
if ($file['error'] != 0) {
var_dump($file);
die;
}
$url = 'http://127.0.0.1:8080/group1/upload';
$params = [];
if (class_exists('\CURLFile')) {
echo 1;die;
$params['file'] = new \CURLFile(realpath($file['tmp_name']), $file['type'], $file['name']);
} else {
$params['file'] = '@' . realpath($file['tmp_name']) . ";type=" . $file['type'] . ";filename=" . $file['name'];
}
$res = curlPost($url, $params);
dump($res);
}
function curlPost(string $url, array $params = []): array
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$output = curl_exec($ch);
$res = ["code" => 200, "msg" => "请求成功", 'data' => $output];
if ($output === false) {
$res = ["code" => 500, "msg" => curl_error($ch)];
}
curl_close($ch);
return $res;
}
|