zoukankan      html  css  js  c++  java
  • php curl上传文件$_FILES为空问题

    php使用curl上传文件,代码如下:

    发送的代码(完全是官方的示例)

    <?php

    /* http://localhost/upload.php:
    print_r($_POST);
    print_r($_FILES);
    */

    $ch = curl_init();

    $data = array('name' => 'Foo', 'file' => '@/home/vagrant/test.png');

    curl_setopt($ch, CURLOPT_URL, 'http://localhost/test/curl/load_file.php');
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

    curl_exec($ch);
    ?>

    接收代码(也是官方的)

    <?php
    print_r($_POST);
    print_r($_FILES);
    运行结果

    php -f demo.php
    Array
    (
    [name] => Foo
    [file] => @/home/vagrant/test.png
    )
    Array
    (
    )

    解决方法1:
    <?php

    /* http://localhost/upload.php:
    print_r($_POST);
    print_r($_FILES);
    */

    $ch = curl_init();

    $data = array('name' => 'Foo', 'file' => '@/home/vagrant/test.png');

    curl_setopt($ch, CURLOPT_URL, 'http://localhost/test/curl/load_file.php');
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

    curl_exec($ch);
    ?>

    解决方法2:
    5.6版本下
    <?php

    /* http://localhost/upload.php:
    print_r($_POST);
    print_r($_FILES);
    */

    $ch = curl_init();

    $data = array('name' => 'Foo', 'file' => new CURLFile(realpath('/home/vagrant/test.png')));

    curl_setopt($ch, CURLOPT_URL, 'http://localhost/test/curl/load_file.php');
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

    curl_exec($ch);
    ?>

    相关文章:

    php curl文件上传兼容php5.0~5.6各版本

    http://www.cnblogs.com/zqifa/p/php-curl-2.html

  • 相关阅读:
    文件传输协议FTP
    过河问题 还是不会 去学请教一下 数学老师 -----
    ----堆栈 STL 函数库 ----有待补充
    UVa 101
    例题 5-1 STL
    课后题 3-3 水题
    第八届河南省省赛 A.挑战密室
    课后题--------求分子量-----Molar mass------
    课后题3-1
    ---------快排-----表排-----基数排序(桶排序)-----
  • 原文地址:https://www.cnblogs.com/zqifa/p/php-curl-1.html
Copyright © 2011-2022 走看看