CULR 部分
$url = 'http://localhost/test/curlUploadFile/upload.php'; //处理上传的php文件,根据情况修改 $path='ftp.txt'; //文件路径 $curl = curl_init(); curl_setopt($curl,CURLOPT_SAFE_UPLOAD,true); //$data = array('file' => '@' . realpath($path));//‘@' 符号告诉服务器为上传资源 php<=5.5 这个是php版本小于5.5的用法 $data = ['file'=>new CURLFile(realpath($path))]; //注意:这里的的 file 可以自定义 curl_setopt($curl,CURLOPT_URL,$url); curl_setopt($curl,CURLOPT_POST,1); curl_setopt($curl,CURLOPT_POSTFIELDS,$data); curl_setopt($curl,CURLOPT_RETURNTRANSFER,1); curl_setopt($curl,CURLOPT_USERAGENT,'test'); echo $res=curl_exec($curl); echo $err = curl_error($curl);
upload.php部分:
$file = $_FILES; file_put_contents(time().'.json',json_encode($_FILES)); $tmpname = $_FILES['file']['tmp_name']; $name = $_FILES['file']['name']; $res = move_uploaded_file($tmpname,'uploads/'.$name); var_dump($res);