簡體   English   中英

使用cURL的PHP​​遠程文件上傳

[英]PHP remote file upload using 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

我上傳文件的位置不正確嗎?

使用 file_get_contents('php://input') 獲取帖子數據

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

文件路徑“public_html/admin/billing/voip_daily_cdr_files/”不正確試試這個“/billing/voip_daily_cdr_files”如果不起作用你應該檢查正確的文件路徑。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM