首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

CURLFile::__construct

(PHP 5 >= 5.5.0, PHP 7)

CURLFile::__construct -- curl_file_create — Create a CURLFile object

描述

面向对象的风格

代码语言:javascript
复制
public CURLFile::__construct ( string $filename [, string $mimetype [, string $postname ]] )

程序风格

代码语言:javascript
复制
CURLFile curl_file_create ( string $filename [, string $mimetype [, string $postname ]] )

创建一个CURLFile对象,用于上传文件CURLOPT_POSTFIELDS

参数

filename

将要上传的文件的路径。

mimetype

文件的Mimetype。

postname

上传数据中要使用的文件的名称。

返回值

返回一个CURLFile对象。

例子

Example #1 CURLFile::__construct() example

面向对象的风格

代码语言:javascript
复制
<?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);
?>

程序风格

代码语言:javascript
复制
<?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);
?>

上面的例子将输出:

代码语言:javascript
复制
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)
  }
}

← CURLFile

CURLFile::getFilename →

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com