谁能给我举一个PHP';什么是卷发文件类?

谁能给我举一个PHP';什么是卷发文件类?,php,curl,upload,Php,Curl,Upload,我有一个非常简单的PHP代码将文件上传到远程服务器;我这样做的方式(正如在其他一些解决方案中所建议的那样)是使用cUrl上传文件 这是我的密码: $ch = curl_init("http://www.remotesite.com/upload.php"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIEL

我有一个非常简单的PHP代码将文件上传到远程服务器;我这样做的方式(正如在其他一些解决方案中所建议的那样)是使用cUrl上传文件

这是我的密码:

$ch = curl_init("http://www.remotesite.com/upload.php"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('fileupload' => '@'.$_FILES['Filedata']['tmp_name'])); 
echo curl_exec($ch);        
服务器运行的是PHP 5.5.0,并且在PHP>=5.5.0中@filename似乎已被弃用,如
CURLOPT_POSTFIELDS
描述中所述,因此,我收到以下错误:

Deprecated: curl_setopt(): The usage of the @filename API for file uploading is deprecated. Please use the CURLFile class instead in ... 
有趣的是,除了一个基本的类概述之外,php.net上完全没有关于这个类的内容。没有示例,没有方法或属性的描述。基本上是空白的。我知道这是一个全新的类,几乎没有文档,几乎没有实际用途,这就是为什么在谷歌或Stackoverflow上的搜索中几乎没有相关内容出现在这个类上的原因

我想知道是否有人使用过这个CURLFile类,可以帮助我或者给我一个例子,在我的代码中用它代替@filename

编辑:

我还想添加我的“upload.php”代码;此代码将使用传统的@filename方法,但不再使用CURLFile类代码:

$folder = "try/";
$path = $folder . basename( $_FILES['file']['tmp_name']); 
if(move_uploaded_file($_FILES['file']['tmp_name'], $path)) {
echo "The file ".  basename( $_FILES['file']['tmp_name']). " has been uploaded";
}
最终编辑

想要为其他寻找几乎没有文档记录的CURLFile类的类似工作示例的人添加最终/工作代码

curl.php(本地服务器)


RFC上有一个代码片段:

如果您害怕创建对象,还可以使用看似毫无意义的函数
curl\u file\u create(string$filename[,string$mimetype[,string$postname]])

curl_setopt($curl_handle, CURLOPT_POST, 1);
$args['file'] = curl_file_create('filename.png', 'image/png', 'filename.png');
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $args);

感谢您的帮助,使用您的工作代码,我能够用PHP5.5和Facebook SDK解决我的问题。我是从sdk类中的代码中得到这个错误的

我不认为这算是一种回应,但我确信有人会像我一样搜索与facebook SDK和PHP5.5相关的错误

如果有人有同样的问题,我的解决方案是将base_facebook.php中的一些代码更改为使用CurlFile类而不是@filename

由于我从多个地方调用sdk,我只修改了sdk的几行:

在名为“makeRequest”的方法中,我做了以下更改:

在守则的这部分:

if ($this->getFileUploadSupport()){
    $opts[CURLOPT_POSTFIELDS] = $params;
} else {
    $opts[CURLOPT_POSTFIELDS] = http_build_query($params, null, '&');
}
将第一部分(启用文件上载)更改为:

也许这可以通过解析每个$param并在值中查找“@”来改进。。但我这样做只是为了图像,因为这正是我所需要的。

对于curl_setopt():不赞成使用@filename API上传文件。请改用CURLFile类

$img='image.jpg';

$data_array = array(
        'board' => $board_id,
        'note' => $note,
        'image' => new CurlFile($img)
    );

$curinit = curl_init($url);
     curl_setopt($curinit, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($curinit, CURLOPT_POST, true);
     curl_setopt($curinit, CURLOPT_CUSTOMREQUEST, "POST");
     curl_setopt($curinit, CURLOPT_POSTFIELDS, $data_array);
     curl_setopt($curinit, CURLOPT_SAFE_UPLOAD, false);
     $json = curl_exec($curinit);
     $phpObj = json_decode($json, TRUE);  
     return $phpObj;

CURLFile在上面已经解释得很清楚了,但是对于不想发送多部分消息的简单的单文件传输(一个文件不需要,一些API不支持多部分),下面的方法就行了

$ch = curl_init('https://example.com');

$verbose = fopen('/tmp/curloutput.log', 'w+'); // Not for production, but useful for debugging curl issues.

$filetocurl = fopen(realpath($filename), 'r');

// Input the filetocurl via fopen, because CURLOPT_POSTFIELDS created multipart which some apis do not accept.

// Change the options as needed.

$options = array(
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_HTTPHEADER => array(
        'Content-type: application/whatever_you_need_here',
        'Authorization: Basic ' . $username . ":" . $password) // Use this if you need password login
    ),
    CURLOPT_NOPROGRESS => false,
    CURLOPT_UPLOAD => 1,
    CURLOPT_TIMEOUT => 3600,
    CURLOPT_INFILE => $filetocurl,
    CURLOPT_INFILESIZE => filesize($filename),
    CURLOPT_VERBOSE => true,
    CURLOPT_STDERR => $verbose  // Remove this for production
);

if (curl_setopt_array($ch, $options) !== false) {
    $result = curl_exec($ch);
    $info = curl_getinfo($ch);
    curl_close($ch);
} else {
    // A Curl option could not be set. Set exception here
}

请注意,上面的代码有一些额外的调试功能,一旦开始工作就删除它们。

Php POST request使用curl函数发送多个文件:

<?php
    $file1 = realpath('ads/ads0.jpg');
    $file2 = realpath('ads/ads1.jpg');
    // Old method
    // Single file
    // $data = array('name' => 'Alexia', 'address' => 'Usa', 'age' => 21, 'file' => '@'.$file1);    
    // $data = array('name' => 'Alexia', 'address' => 'Usa', 'age' => 21, 'file[0]' => '@'.$file1, 'file[1]' => '@'.$file2);
    // CurlFile method   
    $f1 = new CurlFile($file1, mime_content_type($file1), basename($file1)); 
    $f2 = new CurlFile($file2, mime_content_type($file2), basename($file2)); 
    $data = array('name' => 'Alexia', 'address' => 'Usa', 'age' => 21, 'file[1]' => $f1, 'file[2]' => $f2);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://url.x/upload.php');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false); // !!!! required as of PHP 5.6.0 for files !!!
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);    
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // 1, 2
    curl_setopt($ch, CURLOPT_TIMEOUT, 60);
    // curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    $res2 = curl_exec($ch);
    echo $res2;
?>

<?php
    // upload.php
    $json = json_decode(file_get_contents('php://input'), true);
    if(!empty($json)){ print_r($json); }
    if(!empty($_GET)){ print_r($_GET); }
    if(!empty($_POST)){ print_r($_POST); }
    if(!empty($_FILES)){ print_r($_FILES); }
?>


Well-我粘贴的部分几乎是文档中没有的所有有用部分。该页面确实提供了该类的实际使用情况的更有用的视图;并添加$args['file']=new CurlFile($_FILES['Filedata']['tmp_name']);curl_setopt($ch,CURLOPT_POSTFIELDS,$args);摆脱了不推荐使用的错误;事实上,它一起消除了错误。我现在的问题是,另一端的“upload.php”已经失效。我不确定我需要对它做什么更改才能接收文件-因为它实际上不再接收文件了。如果(version\u compare(PHP\u version,'5.5.0')>=0)到您的代码中,您可以添加
一个问题。。。当我试图捕获我的文件时,它没有出现在$\u file['file']变量中,而是出现在$\u POST['file']中,你知道为什么吗?你可以在upload.php中使用var\u dump($\u FILES)?如果我这样做了,我如何让它在curl.php页面上返回结果?我之前试着这么做,但一旦我执行提交代码,实际上什么都不会返回。好的-必须将RETURNTRANSFER设置为true;下面是upload.php下的var_dump($_FILES)的结果:
array(1){[“file”]=>array(5){[“name”]=>string(11)“phpC0F4.tmp”[“type”]=>string(24)“application/octet stream”[“tmp_name”]=>string(14)”/tmp/phpj7C3Hv”[“error”=>int(0)[“size”=>int(483397)}文件phpj7C3Hv已上载
我99%工作-感谢您。唯一缺少的是扩展。我需要做几件事——在curl.php中,我将代码更改为
$args['file']=new CurlFile($\u FILES['Filedata']['tmp\u name'],'file/exgpd',$RealTitleID)
,以包括mime类型和postname。在upload.php中,我将代码更改为
$path=$folder$_文件['file']['name']
。此时文件正在上载到远程服务器;唯一的问题是它没有保留扩展名,只保留文件名。我只需要找出这一部分。感谢男人wtf一个简单的卷曲图像文件上传花了我1个小时的时间来找出哦,很有用;谢谢FWIW,我认为完全放弃
@
格式并将应用程序代码更改为使用
CurlFile()
方法可能更有意义。但这还需要讨论。不管怎样,谢谢!当使用CurlFile技术时,不应该设置CURLOPT_SAFE_UPLOAD(它默认为true,应该保持这种方式),所以应该删除该行。PHP7中甚至不再包含CURLOPT_SAFE_上传。
if ($this->getFileUploadSupport()){
  if(!empty($params['source'])){
     $nameArr = explode('/', $params['source']);
     $name    = $nameArr[count($nameArr)-1]; 
     $source  = str_replace('@', '', $params['source']);
     $size    = getimagesize($source); 
     $mime    = $size['mime'];
     $params['source'] = new CurlFile($source,$mime,$name);
  }

  if(!empty($params['image'])){
     $nameArr = explode('/', $params['image']);
     $name    = $nameArr[count($nameArr)-1]; 
     $image   = str_replace('@', '', $params['image']);
     $size    = getimagesize($image); 
     $mime    = $size['mime'];
     $params['image'] = new CurlFile($image,$mime,$name);
  }
  $opts[CURLOPT_POSTFIELDS] = $params;
} else {
    $opts[CURLOPT_POSTFIELDS] = http_build_query($params, null, '&');
}
$img='image.jpg';

$data_array = array(
        'board' => $board_id,
        'note' => $note,
        'image' => new CurlFile($img)
    );

$curinit = curl_init($url);
     curl_setopt($curinit, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($curinit, CURLOPT_POST, true);
     curl_setopt($curinit, CURLOPT_CUSTOMREQUEST, "POST");
     curl_setopt($curinit, CURLOPT_POSTFIELDS, $data_array);
     curl_setopt($curinit, CURLOPT_SAFE_UPLOAD, false);
     $json = curl_exec($curinit);
     $phpObj = json_decode($json, TRUE);  
     return $phpObj;
$ch = curl_init('https://example.com');

$verbose = fopen('/tmp/curloutput.log', 'w+'); // Not for production, but useful for debugging curl issues.

$filetocurl = fopen(realpath($filename), 'r');

// Input the filetocurl via fopen, because CURLOPT_POSTFIELDS created multipart which some apis do not accept.

// Change the options as needed.

$options = array(
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_HTTPHEADER => array(
        'Content-type: application/whatever_you_need_here',
        'Authorization: Basic ' . $username . ":" . $password) // Use this if you need password login
    ),
    CURLOPT_NOPROGRESS => false,
    CURLOPT_UPLOAD => 1,
    CURLOPT_TIMEOUT => 3600,
    CURLOPT_INFILE => $filetocurl,
    CURLOPT_INFILESIZE => filesize($filename),
    CURLOPT_VERBOSE => true,
    CURLOPT_STDERR => $verbose  // Remove this for production
);

if (curl_setopt_array($ch, $options) !== false) {
    $result = curl_exec($ch);
    $info = curl_getinfo($ch);
    curl_close($ch);
} else {
    // A Curl option could not be set. Set exception here
}
<?php
    $file1 = realpath('ads/ads0.jpg');
    $file2 = realpath('ads/ads1.jpg');
    // Old method
    // Single file
    // $data = array('name' => 'Alexia', 'address' => 'Usa', 'age' => 21, 'file' => '@'.$file1);    
    // $data = array('name' => 'Alexia', 'address' => 'Usa', 'age' => 21, 'file[0]' => '@'.$file1, 'file[1]' => '@'.$file2);
    // CurlFile method   
    $f1 = new CurlFile($file1, mime_content_type($file1), basename($file1)); 
    $f2 = new CurlFile($file2, mime_content_type($file2), basename($file2)); 
    $data = array('name' => 'Alexia', 'address' => 'Usa', 'age' => 21, 'file[1]' => $f1, 'file[2]' => $f2);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://url.x/upload.php');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false); // !!!! required as of PHP 5.6.0 for files !!!
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);    
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // 1, 2
    curl_setopt($ch, CURLOPT_TIMEOUT, 60);
    // curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    $res2 = curl_exec($ch);
    echo $res2;
?>

<?php
    // upload.php
    $json = json_decode(file_get_contents('php://input'), true);
    if(!empty($json)){ print_r($json); }
    if(!empty($_GET)){ print_r($_GET); }
    if(!empty($_POST)){ print_r($_POST); }
    if(!empty($_FILES)){ print_r($_FILES); }
?>