上传文件和POST非常相似,都是通过表单采取POST方法提交的。
先新建一个接收文件的页面upload.php:
var_dump($_POST); var_dump($_FILES);
再建立一个发送文件的test.php
$post_data = array( ‘name‘ => ‘chenqionghe‘, ‘logo‘ => ‘@E:\test.jpg‘, ‘face‘ => ‘handsome‘ ); //1.初始化 $ch = curl_init(); //2.设置选项,包括URL curl_setopt($ch, CURLOPT_URL,"http://localhost/upload.php"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); //3.执行并获取内容 $output = curl_exec($ch); //4.释放curl句柄 //$info = curl_getinfo($ch);//获取一个cURL连接资源句柄的信息 curl_close($ch); echo $output;
访问test.php,显示结果如下
array (size=2) ‘name‘ => string ‘chenqionghe‘ (length=11) ‘face‘ => string ‘handsome‘ (length=8) array (size=1) ‘logo‘ => array (size=5) ‘name‘ => string ‘test.jpg‘ (length=8) ‘type‘ => string ‘application/octet-stream‘ (length=24) ‘tmp_name‘ => string ‘D:\Wamp\tmp\php67A3.tmp‘ (length=23) ‘error‘ => int 0 ‘size‘ => int 57083
时间: 2024-11-14 21:30:32