php simulates file upload using curl to upload files to the remote server, and php converts the picture into a binary file for request interface upload

The native phpCURLFile class uploads files to the server, code

// 要上传的文件路径
$file_path = '/path/to/file.txt';
// 远程服务器接收文件的 API 地址
$upload_url = 'http://example.com/upload.php';
// 创建 CURLFile 对象
$file = new CURLFile($file_path);
// 构建 POST 数据
$data = array(
    'file' => $file,
    'name' => basename($file_path),
);
// 创建 CURL 请求
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $upload_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// 发送请求并获取响应
$response = curl_exec($ch);
// 检查是否上传成功
if ($response === false) {
    
    
    echo '上传失败:' . curl_error($ch);
} else {
    
    
    echo '上传成功:' . $response;
}
// 关闭 CURL 请求
curl_close($ch);

In the code, the CURLFile class** (the source code of the CURLFile class is attached to the end of the article
)** creates a CURL file object. Next build the POST data, passing the file object as the parameter value. Finally use curl to send the request, get the response and check if the upload was successful.

Tp5 version CURLFile class uploads files to the server, code

// 要上传的文件路径
$file_path = '/path/to/file.txt';
// 远程服务器接收文件的 API 地址
$upload_url = 'http://example.com/upload.php';
// 创建 CURLFile 对象
$file = new \CURLFile($file_path);
// 构建 POST 数据
$data = array(
    'file' => $file,
    'name' => basename($file_path),
);
// 创建 CURL 请求
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $upload_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// 发送请求并获取响应
$response = curl_exec($ch);
// 检查是否上传成功
if ($response === false) {
    
    
    echo '上传失败:' . curl_error($ch);
} else {
    
    
    echo '上传成功:' . $response;
}
// 关闭 CURL 请求
curl_close($ch);

In the code, use the TP5 built-in CURLFile class to create a CURL file object.

CURLFile class source code

class CURLFile {
    
    
    private $name;
    private $mime = '';
    private $postname = '';
    public function __construct($filename, $mimetype = '', $postname = '') {
    
    
        $this->name = $filename;
        if ($mimetype) {
    
    
            $this->mime = $mimetype;
        }
        if ($postname) {
    
    
            $this->postname = $postname;
        }
    }
    public function getName($as_array = false) {
    
    
        if ($as_array) {
    
    
            return array('name' => $this->name, 'type' => $this->mime, 'postname' => $this->postname);
        }
        return $this->name;
    }
    public function getMimeType() {
    
    
        return $this->mime;
    }
    public function getPostFilename() {
    
    
        return $this->postname;
    }
    public function __toString() {
    
    
        return sprintf("@%s;filename=%s%s", $this->name, $this->postname, $this->mime ? ";type=" . $this->mime : '');
    }
}

Detailed interpretation of CURLFile class

The CURLFile class is a built-in PHP class used to construct file objects when uploading files using CURL. As can be seen from the source code, the CURLFile class contains the following four methods:

__construct($filename, $mimetype = '', $postname = ''): Constructor, used to create a CURLFile object, need to pass file path, file type and file name parameters.

getName($as_array = false): Get the file path, if the $as_array parameter is true, return an associative array containing the file path, file type and file name.

getMimeType(): Get the file type.

getPostFilename(): Get the file name.

__toString(): Convert the CURLFile object into a string for sending data in CURL POST request.
In short, through the CURLFile class, we can conveniently construct file objects to realize the file upload function.

Guess you like

Origin blog.csdn.net/gjwgjw1111/article/details/129706949