PHP cURL在与远程服务器关系的本地主机上不工作



我目前正在使用PHP cURL从本地主机复制文件到远程服务器。

我有两个独立的文件,一个用于发送文件的本地主机,另一个用于远程服务器接收文件并将其保存到服务器。代码如下:

send.php

<?PHP
$web_page_to_send = "http://admin123.unaux.com/receive.php";
$file_name_with_full_path = "test.jpg";
$post_request = array
(
"sender" => "tmp", 
"file" => curl_file_create($file_name_with_full_path)
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $web_page_to_send);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_request);
$result = curl_exec($ch);
curl_close($ch);
echo "<br>Result: ".$result;
?>

receive.php

<?PHP
if(isset($_POST['sender']))
{
echo "got it !";
$file_name = "tmp/".$_POST['sender']."-".$_FILES['file']['name'];
move_uploaded_file($_FILES['file']['tmp_name'], $file_name);
echo "Successful Attempt! <br><br>Filename: ".$file_name;
echo '<br><br> <img src="'.$file_name.'" width="300px"></img>';
}
else
{
echo 'Unauthorized Access!';
}
?>

代码在本地主机上运行良好,但在将receive.php放置到远程服务器后,该文件不再发送和显示。"http://admin123.unaux.com/receive.php"是文件在远程服务器上的位置。我使用profreehost免费服务器作为远程服务器。

远程服务器文件夹结构:Remote server File and folder structure

有谁能帮我解决这个问题吗?谢谢。

请按如下方式在receive.php文件中获取所有接收到的数据:

$post_data = trim(file_get_contents("php://input"));

在之后,尝试打印$post_data。如果你将得到空值,那么响应没有击中你的文件。试着用下面的代码发送上面的数据:

$web_page_to_send = "http://admin123.unaux.com/receive.php";
$file_name_with_full_path = "test.jpg"; // Please write here full file path in the system
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $web_page_to_send,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 20,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => array('sender' => 'tmp','file'=>new CURLFILE($file_name_with_full_path)),
CURLOPT_HTTPHEADER => array(
"Content-Type: image/jpg"
),
CURLOPT_UPLOAD=>true,
));
$response = curl_exec($curl);
$http_response = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);

相关内容

最新更新