我有一个小的PHP应用程序,里面有一些文件。我想使用curl将一个文件从一个服务器发送到另一个服务器。
我已经在服务器上有文件,我只需将其发送到另一台服务器。
我执行以下代码:
$url = "http://localhost:3919/";
$myCurl = curl_init();
curl_setopt($myCurl, CURLOPT_URL, $url);
curl_setopt_array($myCurl, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HEADER => false,
CURLOPT_SAFE_UPLOAD => true,
// CURLOPT_HTTPHEADER => array(
// "Content-Type: multipart/form-data"),
CURLOPT_POSTFIELDS => array(
"inputFile" => new CurlFile('@' . './Files/example.docx'))
));
$response = curl_exec($myCurl);
curl_close($myCurl);
return $response;
但它并没有触发服务器。那我该怎么办?
例如在其他情况下(其他应用程序)我使用它:
$file = $_FILES["inputFile"];
curl_setopt_array($myCurl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HEADER => false,
CURLOPT_SAFE_UPLOAD => true,
CURLOPT_HTTPHEADER => array(
"Content-Type: multipart/form-data"),
CURLOPT_POSTFIELDS => array(
"inputFile" => new CurlFile($file["tmp_name"], $file["type"], $file["name"]))
));
它完美无缺。
答案 0 :(得分:0)
也许您需要验证帖子上是否存在文件并完成此示例:
<div class="wrap">
<div class="left">
<div class="left-one">
HELLO<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br> HELLO
<br>
</div>
<div class="left-two">
<img src="https://i.imgur.com/pSCepJR.jpg">
</div>
</div>
<div class="right">
<img src="https://i.imgur.com/8UZG4cr.jpg">
</div>
</div>
这是用base64加密文件的一个例子。
您的外部服务器
<form enctype="multipart/form-data" encoding='multipart/form-data' method='post' action="form.php">
<input name="uploadedfile" type="file" value="choose">
<input type="submit" value="Upload">
</form>
<?
if ( isset($_FILES['uploadedfile']) ) {
$filename = $_FILES['uploadedfile']['tmp_name'];
$handle = fopen($filename, "r");
$data = fread($handle, filesize($filename));
$POST_DATA = array(
'file' => base64_encode($data)
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, '<span style="color: red;">http://extserver.com/handle.php</span>');
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);
echo "<h2>File Uploaded</h2>";
}
?>
答案 1 :(得分:0)
Using the curl_file_create works for me on sending files in curl.
$target_url = 'http://localhost:3919/';
$post = array (
'file' => curl_file_create('Location of the file')
);
$ch = curl_init ($target_url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$msg=curl_exec ($ch);
$info = curl_getinfo($ch);
if($info['http_code'] === 200){
$returnMessage = json_decode($msg,1);
} else {
$returnMessage['file_type'] = 'error';
}
curl_close($ch);