开发者社区> 问答> 正文

php cURL 上传文件失败,是怎么回事?

curlPost.php

 <?php

    $post = array(
        'name' => 'wyjie',
        'img'  => '@D:/debug.exe'
        );

    //cURL发送post请求
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://localhost/demo/demo/curl/01.php');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

    $optput = curl_exec($ch);
    curl_close($ch);

    echo $optput;

 ?>

01.php

 <?php
    print_r($_POST);
    print_r($_FILES);

 ?>

输出结果
Array ( [name] => wyjie [img] => @./debug.exe ) Array ( )

展开
收起
小旋风柴进 2016-03-09 14:30:16 3648 0
1 条回答
写回答
取消 提交回答
  • PHP 在5.4之后 不支持 @D:/debug.exe 的写法 。

    所有为了兼容 ,可以这样写

        if (class_exists('\CURLFile')) {
                $param['file'] = new \CURLFile(realpath($file));
        } else {
                $param['file'] = '@' . realpath($file);
        }
    2019-07-17 18:56:07
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
PHP安全开发:从白帽角度做安全 立即下载
PHP 2017.北京 全球开发者大会——高可用的PHP 立即下载
复杂PHP系统性能瓶颈排查及优化 立即下载