我尝试了以下代码,使用php将图像从一台服务器上传到另一台服务器。但是无法将图像上传到目标服务器。如果我的代码有任何错误,请告诉我。
let promisesToAwait = [];
let words = req.body.words;
for (let j = 0; j < words.length; j++) {
promisesToAwait.push(await this.loopThruAllWords(words[j], req.body.domains.join(', ')));
}
let allResponses = await Promise.all(promisesToAwait1);
async loopThruAllWords(word, domains){
let response = await newsapi.v2.everything({
q: word,
domains: domains,
sort_by: 'relevancy'
});
return Promise.resolve(response);
}
中使用的代码,该代码位于我的源服务器上:
form.php
以及我的<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, 'https://www.kanchikart.in/img/handle.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);
echo "<h2>File Uploaded</h2>";
}
?>
在目标服务器上:
handler.php
尝试上述代码后,我没有将图像存储在目标服务器中。
答案 0 :(得分:0)
在处理https
端点时,您需要在curl请求中设置其他选项-特别是有效的证书...您可以从curl.haxx.se
测试响应或使用info
(http_code)确定成功或失败。以下内容似乎对我有用,并产生了200个响应代码〜检查是否有新文件
<?php
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 )
);
$cacert='c:/wwwroot/cacert.pem';
$url='https://www.kanchikart.in/img/handle.php';
$curl = curl_init();
if( parse_url( $url,PHP_URL_SCHEME )=='https' ){
curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, true );
curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, 2 );
curl_setopt( $curl, CURLOPT_CAINFO, $cacert );
}
curl_setopt($curl, CURLOPT_URL, $url );
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);
$info = (object)curl_getinfo( $curl );
curl_close ($curl);
echo ( $info->http_code==200 ) ? "<h2>File Uploaded</h2>" : "<h2>Bogus</h2>";
}
?>
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title>cURL - Server to Server....</title>
</head>
<body>
<form enctype="multipart/form-data" encoding='multipart/form-data' method='post'>
<input name="uploadedfile" type="file" value="choose">
<input type="submit" value="Upload">
</form>
</body>
</html>