使用cURL上传PHP远程文件

时间:2015-04-15 07:41:20

标签: php curl

我有这个PHP代码,它发布一个文件以上传到远程服务器:

$filename  = $at[attachment];
$handle    = fopen($filename, "r");
$data      = fread($handle, filesize($filename));
$POST_DATA = array(
'file' => base64_encode($data), 
'location' => 'billing/voip_daily_cdr_files'
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://www.domain.co.uk/file_upload.php');
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $POST_DATA);
$response = curl_exec($curl);
curl_close ($curl);

然后file_upload.php看起来像:

$encoded_file = $_POST['file'];
$decoded_file = base64_decode($encoded_file);
file_put_contents($_POST["location"].'/'.$decoded_file, $decoded_file);

我做了一个var_dump($ response);看到输出并看到这个:

Warning: file_put_contents(public_html/admin/billing/voip_daily_cdr_files/): failed to open stream: No such file or directory in /home/user/public_html/admin/file_upload.php on line 4

我的位置不正确,无法将文件上传到?

2 个答案:

答案 0 :(得分:0)

使用file_get_contents('php:// input')获取发布数据

<?php
     $encoded_file  = file_get_contents('php://input');// this is you for getting post data
     print_r($encoded_file );
    }

答案 1 :(得分:0)

文件路径&#34; public_html / admin / billing / voip_daily_cdr_files /&#34;不正确试试这个 &#34; /计费/ voip_daily_cdr_files&#34;如果不起作用,你应该检查正确的文件路径。