php7中使用curl上传文件

上传文件代码:


<?php
 
	$url = 'http:/abc.com/uploadfile.php';
	$post_data = [
		'file' => new CURLFile(realpath('a.txt'))
	];
	$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, $post_data);
	$output = curl_exec($ch);
	curl_close($ch);
	echo $output;

这里的file必须要使用new CURLFile(),并且可以加**realpath()**函数,因为PHP7版本后,都必须使用绝对路径!不用realpath()可以换成 $_FILES[“file”][“tmp_name”]
uploadfile.php代码:

<?php
	echo json_encode($_FILES);
	echo json_encode($_POST);
发布了30 篇原创文章 · 获赞 23 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_41716624/article/details/102800962