这是我的代码,我正在尝试使用其api将文件上传到nextcloud,当我在邮递员中调用我的操作时,它给了我200好的代码,但没有文件上传
if(isset($_POST['submit'])) {
$filename = $_FILES['file']['name'];
$filedata = $_FILES['file']['tmp_name'];
if ($filedata != '') {
$username = "user";
$password = "pass";
$headers = array('Authorization: Basic ' . base64_encode("$username:$password"), 'OSC-APIRequest: true'); // cURL headers for file uploading
$cfile = new \CURLFile($filedata);
$postfields = array($filename => $cfile);
$url = '10.8.0.45/remote.php/dav/files/admin/' . basename($filedata);
$curl = curl_init();
$options = array(
CURLOPT_URL => $url,
CURLOPT_HEADER => true,
CURLOPT_PUT => true,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => http_build_query($postfields),
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_RETURNTRANSFER => true,
);
curl_setopt_array($curl, $options);
curl_exec($curl);
$response = curl_getinfo($curl);
curl_close($curl);
echo $response;
}
}
使用此代码,我可以将文件上传到云中,但是它上传为空,有什么想法吗?
$username = "user";
$password = "pass";
$headers = array('Authorization: Basic ' . base64_encode("$username:$password"), 'OSC-APIRequest: true'); // cURL headers for file uploading
$carpetaModulo = dirname(__DIR__);// path to the module
$carpetaTemp = $carpetaModulo. '\temp_files';
$file = scandir($carpetaTemp);
$filename = $file[2];
$path = $carpetaTemp.'/'.$filename;
$finfo = new \finfo(FILEINFO_MIME_TYPE);
$mimetype = $finfo->file($path);
$cfile = curl_file_create($path, $mimetype, $filename);
$url = '10.8.0.45/remote.php/dav/files/admin/' . $filename;
$curl = curl_init();
$options = array(
CURLOPT_URL => $url,
CURLOPT_HEADER => true,
CURLOPT_PUT => true,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => array($cfile),
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_RETURNTRANSFER => true,
);
curl_setopt_array($curl, $options);
$response = curl_exec($curl);
curl_close($curl);
return $response;