使用curl发送文件

时间:2013-08-19 10:15:49

标签: php curl

我的public_html文件夹中有一个文件。我想通过卷曲将文件发送到其他网址。我正在读取文件的二进制代码并尝试发送到另一个URL。但是在接收端我无法获取文件,但我能够获得其他参数,如from,to和subject但不是文件。下面是阅读和发送文件的代码。

// ini attachments arrar
$arrAttachments = array();
echo 'attaching files ...<br>';
//dummy attachments files 
$arrFile = array('0'=>'running.jpg',
                 '1'=>'mail-doc.docx',
                 '2'=>'mail-sheet.xlsx',
                );
//get the binary of each attachments file
if(is_array($arrFile) && count($arrFile)>1) {

echo 'reading  files ...<br>';  
foreach ($arrFile as $key => $value) {
    //file location 
    $file = 'att/'.$value;
    $handle = fopen($file, "rb");

    //file size
    $fsize = filesize($file);

    //read file 
    $contents = fread($handle, $fsize);

    fclose($handle);

    //make binary data array 
    $arrAttachments[$key]['file_name']  =  $value;
    $arrAttachments[$key]['file_value'] = $contents;
}   
}

echo 'making data for post ...<br>';    
//whole email body with header
$emailContent  = array(
                        'attachments'=>json_encode($arrAttachments),    
                        'from'=>'mado023@gmail.com',
                        'to'=>'1234@customer3sixty.com',
                        'subject'=>'This is subject',
                        'body'=>'This is body',
                        );

echo 'curl request ini ...<br>';                    
//mail parser url 
$url ='http://localhost/c360/cron/t/';
//send the email content to url via curl
$ch = curl_init($url); //ini curl
curl_setopt_array($ch,array(
                            CURLOPT_RETURNTRANSFER => 1,
                            CURLOPT_TIMEOUT => 50,
                            CURLOPT_CONNECTTIMEOUT => 0,
                            CURLOPT_SSL_VERIFYHOST => 0,
                            CURLOPT_SSL_VERIFYPEER => 0
                            )
                            );
//set the post filed                            
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($emailContent));

//execute curl 
$response = curl_exec($ch);

在接收端我尝试记录传入的数据.Below是接收参数和文件的代码。

$c .= 'from=>'.$_POST['from'] .' ';
        $c .= 'to=>'.$_POST['to'] .' ';
        $c .= 'subject=>'.$_POST['subject'] .' ';
        $c .= 'body=>'.$_POST['body'] .' ';
        //$c .= 'attachments=>'.json_encode($_POST['attachments']).' ';
        foreach ($_POST['attachments'] as $key => $value) {
            $c .= $value;
        }

        file_put_contents('pipe.log', $c);

0 个答案:

没有答案