Book/curl-Phpdoc专题

Client URL 库

目录

简介

CURLFile 应该与选项 CURLOPT_POSTFIELDS 一同使用用于上传文件。

类摘要

CURLFile

class CURLFile {

/* 属性 */

public $name ;

public $mime ;

public $postname ;

/* 方法 */

public <span class="methodname">__construct ( <span class="methodparam">string $filename [, string $mimetype [, <span class="type">string $postname ]] )

public string getFilename ( <span class="methodparam">void )

public string getMimeType ( <span class="methodparam">void )

public string getPostFilename ( <span class="methodparam">void )

public void setMimeType ( <span class="methodparam">string $mime )

public void setPostFilename ( <span class="methodparam">string $postname )

}

属性

name
待上传文件的名称

mime
文件的 MIME type(默认是application/octet-stream)。

postname
上传数据中的文件名称(默认为属性 name )。

参见

  • curl_setopt

CURLFile::__construct

curl_file_create

创建 CURLFile 对象

说明

面向对象风格

public <span class="methodname">CURLFile::__construct ( <span class="methodparam">string $filename [, string $mimetype [, <span class="type">string $postname ]] )

过程化风格

CURLFile <span class="methodname">curl_file_create ( <span class="methodparam">string $filename [, string $mimetype [, <span class="type">string $postname ]] )

创建 CURLFile 对象,使用 CURLOPT_POSTFIELDS 选项上传文件。

参数

filename
被上传文件的 路径。

mimetype
被上传文件的 MIME 类型。

postname
上传数据里面的文件名。

返回值

返回 CURLFile 对象。

范例

示例 #1 CURLFile::__construct 示例

面向对象风格

<?php
/* http://example.com/upload.php:
<?php var_dump($_FILES); ?>
*/

// Create a cURL handle
$ch = curl_init('http://example.com/upload.php');

// Create a CURLFile object
$cfile = new CURLFile('cats.jpg','image/jpeg','test_name');

// Assign POST data
$data = array('test_file' => $cfile);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

// Execute the handle
curl_exec($ch);
?>

过程化风格

<?php
/* http://example.com/upload.php:
<?php var_dump($_FILES); ?>
*/

// Create a cURL handle
$ch = curl_init('http://example.com/upload.php');

// Create a CURLFile object
$cfile = curl_file_create('cats.jpg','image/jpeg','test_name');

// Assign POST data
$data = array('test_file' => $cfile);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

// Execute the handle
curl_exec($ch);
?>

以上例程会输出:

array(1) {
  ["test_file"]=>
  array(5) {
    ["name"]=>
    string(9) "test_name"
    ["type"]=>
    string(10) "image/jpeg"
    ["tmp_name"]=>
    string(14) "/tmp/phpPC9Kbx"
    ["error"]=>
    int(0)
    ["size"]=>
    int(46334)
  }
}

参见

  • curl_setopt

CURLFile::getFilename

获取被上传文件的 文件名

说明

public string CURLFile::getFilename ( <span class="methodparam">void )

参数

此函数没有参数。

返回值

返回被上传文件的 文件名。

CURLFile::getMimeType

获取被上传文件的 MIME 类型

说明

public string CURLFile::getMimeType ( <span class="methodparam">void )

参数

此函数没有参数。

返回值

返回被上传文件的 MIME 类型。

CURLFile::getPostFilename

获取 POST 请求时使用的 文件名

说明

public string CURLFile::getPostFilename ( <span class="methodparam">void )

参数

此函数没有参数。

返回值

返回 POST 请求时使用的 文件名。

CURLFile::setMimeType

设置被上传文件的 MIME 类型

说明

public void CURLFile::setMimeType ( <span class="methodparam">string $mime )

参数

mime
POST 数据中的 MIME 类型。

返回值

没有返回值。

CURLFile::setPostFilename

设置 POST 请求时使用的文件名

说明

public void CURLFile::setPostFilename ( <span class="methodparam">string $postname )

参数

postname
POST 数据里使用的文件名。

返回值

没有返回值。


本站为非盈利网站,作品由网友提供上传,如无意中有侵犯您的版权,请联系删除