作为程序员一定要保持良好的睡眠,才能好编程

curl实现文件上传

发布时间:2018-12-24


curl实现文件上传

<?php

function makeCurlFile($file){
	$mime = mime_content_type($file);
	//var_dump($mime);
	$info = pathinfo($file);
	$name = $info['basename'];
	//var_dump($name);
	$output = new CURLFile($file, $mime, $name);
	return $output;
}


$ch = curl_init("http://172.28.66.198/test/curlupload.php");
$mp3 =makeCurlFile('./music.aac');
$photo = makeCurlFile('./music.jpg');
$data = array('mp3' => $mp3, 'picture' => $photo, 'name' => 'My latest single', 'description' => 'Check out my newest song');
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
if (curl_errno($ch)) {
   $result = curl_error($ch);
}
var_dump($result);
curl_close ($ch);



curl版本化:

<=5.4 curl上传文件只支持@语法

= 5.5 支持@语法和CURLFile类

大于=5.6 只支持CURLFile类


$curl = curl_init();

if (class_exists('\CURLFile')) {// 这里用特性检测判断php版本
     curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
        $data = array('file' => new \CURLFile(realpath($source)));//>=5.5
 } else {
     if (defined('CURLOPT_SAFE_UPLOAD')) {
         curl_setopt($curl, CURLOPT_SAFE_UPLOAD, false);
     }
     $data = array('file' => '@' . realpath($source));//<=5.5
 }

 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");
 $result = curl_exec($curl);
 $error = curl_error($curl);





curlupload.php

<?php


print_r($_POST);

print_r($_FILES);

看执行结果:

D:\phpStudy\WWW\test\curl_uploadfile.php:5:
string(9) "video/mp4"
D:\phpStudy\WWW\test\curl_uploadfile.php:8:
string(9) "music.aac"
D:\phpStudy\WWW\test\curl_uploadfile.php:5:
string(10) "image/jpeg"
D:\phpStudy\WWW\test\curl_uploadfile.php:8:
string(9) "music.jpg"
Array
(
    [name] => My latest single
    [description] => Check out my newest song
)

#把两个文件都已经上传上来了
Array
(
    [mp3] => Array
        (
            [name] => music.aac
            [type] => video/mp4
            [tmp_name] => C:\Windows\php9E46.tmp
            [error] => 0
            [size] => 1776415
        )

    [picture] => Array
        (
            [name] => music.jpg
            [type] => image/jpeg
            [tmp_name] => C:\Windows\php9E86.tmp
            [error] => 0
            [size] => 133140
        )

)

有了$_FILES这个属性,我们就可以通过 move_upload_file 进行上传了。



第二种:

curl采用base64的方式进行上传


 
$ch = curl_init("http://172.28.66.198/test/curluploadsave.php");

$path=__DIR__;

// $data = array('img'=>'@'.$path.DIRECTORY_SEPARATOR.'music.jpg');
$data = array('img'=>base64_encode(file_get_contents($path.DIRECTORY_SEPARATOR.'music.jpg')));

curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_POST,true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

$result = curl_exec($ch);
if (curl_errno($ch)) {
   $result = curl_error($ch);
}
curl_close ($ch);
print_r($result);


curluploadsave.php

<?php


file_put_contents('test.jpg',base64_decode($_POST['img']));

这样也是可以进行上传的。




还有一种方式就是通过ftp进行上传:

<?php
 
$localfile = "music.jpg";
$fp = fopen ($localfile, "r");
$arr_ip = '172.28.66.194';

$ftp = "ftp://".$arr_ip."/vftp/".$localfile; 
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_USERPWD, 'ftp_test:song654321');
curl_setopt($ch, CURLOPT_URL, $ftp);
curl_setopt($ch, CURLOPT_PUT, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
$http_result = curl_exec($ch);
$error = curl_error($ch);
var_dump($error)."<br>";
$http_code = curl_getinfo($ch ,CURLINFO_HTTP_CODE);
curl_close($ch);
fclose($fp);

var_dump($http_result);


执行过程:

*   Trying 172.28.66.194...
* Connected to 172.28.66.194 (172.28.66.194) port 21 (#0)
< 220 (vsFTPd 2.2.2)
> USER ftp_test
< 331 Please specify the password.
> PASS song654321
< 230 Login successful.
> PWD
< 257 "/"
* Entry path is '/'
> CWD vftp
* ftp_perform ends with SECONDARY: 0
< 250 Directory successfully changed.
> EPSV
* Connect data stream passively
< 229 Entering Extended Passive Mode (|||58536|).
*   Trying 172.28.66.194...
* Connecting to 172.28.66.194 (172.28.66.194) port 58536
* Connected to 172.28.66.194 (172.28.66.194) port 21 (#0)
> TYPE I
< 200 Switching to Binary mode.
> STOR music.jpg
< 150 Ok to send data.
* We are completely uploaded and fine
* Remembering we are in dir "vftp/"
< 226 Transfer complete.
* Connection #0 to host 172.28.66.194 left intact
D:\phpStudy\WWW\test\curl_ftpupload.php:18:
string(0) ""
D:\phpStudy\WWW\test\curl_ftpupload.php:23:
string(0) ""
[Finished in 0.2s]

vftp.png