php는 원격 서버에 파일을 업로드하기 위해 curl을 사용하여 파일 업로드를 시뮬레이션하고, php는 요청 인터페이스 업로드를 위해 사진을 이진 파일로 변환합니다.

기본 phpCURLFile 클래스는 파일을 서버에 업로드합니다.

// 要上传的文件路径
$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);

코드에서 CURLFile 클래스**(CURLFile 클래스의 소스 코드는 기사 끝에 첨부됨
)**는 CURL 파일 객체를 생성합니다. 다음으로 파일 개체를 매개 변수 값으로 전달하여 POST 데이터를 빌드합니다. 마지막으로 curl을 사용하여 요청을 보내고 응답을 받고 업로드가 성공했는지 확인합니다.

Tp5 버전 CURLFile 클래스는 파일을 서버, 코드에 업로드합니다.

// 要上传的文件路径
$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);

코드에서 TP5 내장 CURLFile 클래스를 사용하여 CURL 파일 개체를 만듭니다.

CURLFile 클래스 소스 코드

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 : '');
    }
}

CURLFile 클래스의 자세한 해석

CURLFile 클래스는 CURL을 사용하여 파일을 업로드할 때 파일 객체를 구성하는 데 사용되는 내장 PHP 클래스입니다. 소스 코드에서 볼 수 있듯이 CURLFile 클래스에는 다음 네 가지 메서드가 포함되어 있습니다.

__construct($filename, $mimetype = '', $postname = ''): CURLFile 객체를 생성하는 데 사용되는 생성자는 파일 경로, 파일 유형 및 파일 이름 매개변수를 전달해야 합니다.

getName($as_array = false): 파일 경로를 가져옵니다. $as_array 매개변수가 true이면 파일 경로, 파일 유형 및 파일 이름을 포함하는 연관 배열을 반환합니다.

getMimeType(): 파일 형식을 가져옵니다.

getPostFilename(): 파일 이름을 가져옵니다.

__toString(): CURL POST 요청에서 데이터를 보내기 위해 CURLFile 객체를 문자열로 변환합니다.
요컨대, CURLFile 클래스를 통해 편리하게 파일 객체를 구성하여 파일 업로드 기능을 실현할 수 있습니다.

Supongo que te gusta

Origin blog.csdn.net/gjwgjw1111/article/details/129706949
Recomendado
Clasificación