首页 > 解决方案 > 通过 curl 发送较大文件的问题

问题描述

整个下午,

通过 curl 发送大文件时遇到一些问题,我们可以发送第一个 5mb 的测试文件,但第二个测试文件是 34mb 并且不发送。我们增加了发送和接收服务器上的帖子和最大上传大小,但仍然没有乐趣。

这是发送脚本:

 function send_files($directory, $file, $dir_parts) {


    $url = 'https://test.com/App_processing/transfer_videos';

    $handle         = fopen($directory."video.mp4", "r");
    $stats          = fstat($handle);
    $data           = stream_get_contents($handle);
    $payload = array(
            'file' => base64_encode($data),
            'parts' => $dir_parts
    );

    $postfields = array( serialize( $payload ) );

    $ssl        = strstr($url, '://', true);
    $port       = ($ssl == 'https') ? 443 : 80;
    $verify_ssl = false;
    $ch         = curl_init();
    $curlConfig = array(
                        CURLOPT_PORT           => $port,
                        CURLOPT_URL            => $url,

                        CURLOPT_SSL_VERIFYPEER => $verify_ssl,
                        CURLOPT_FORBID_REUSE   => true,
                        CURLOPT_FRESH_CONNECT  => true,
                        CURLOPT_FAILONERROR    => false,
                        CURLOPT_VERBOSE        => true,
                        CURLOPT_POST           => true,
                        CURLOPT_RETURNTRANSFER => true,
                        CURLOPT_FOLLOWLOCATION => true,
                        CURLOPT_COOKIESESSION  => true,
                        CURLOPT_POSTFIELDS     => $postfields,
                    );
    $headers = null;
    $apikey = 'testAPIkey';
    $headers = array(
            "apikey: ".$apikey
    );
    $curlConfig[CURLOPT_HTTPHEADER] = $headers;

    curl_setopt_array( $ch, $curlConfig );

    $response = curl_exec( $ch );
}

接收脚本:

public function transfer_videos() {
    $return_arr = array();
    ini_set('max_execution_time', '999');
    ini_set('memory_limit', '512M');
    ini_set('post_max_size', '512M'); 

    if(count($this->post_data) > 0) {
                $image              = $this->post_data['file'];
                $file               = base64_decode($image);

                $filename           = $this->post_data['parts'][2];


                $return_arr = array();


                $office_path    = $this->config->item('_path') . '_videos/';
                $path           = $o_path.$this->post_data['parts'][0]."/".$this->post_data['parts'][1]."/";
                $name           = $filename;


                if( !is_dir( $path ) ) {
                    mkdir( $path, 0750, true );
                }


                try {

                    $return_arr = file_put_contents($path.$name, $file);

                } catch (Exception $e) {
                    $this->curl_helper->error($path.$name, "failed to upload file");
                }

    }

    echo serialize($return_arr);
}

我们得到的当前错误是:

Recv failure: Connection reset by peer

我们到底做错了什么?

标签: phpapachecurl

解决方案


推荐阅读