Issue
SOLVED
This is my code, I’m trying to upload a file to nextcloud using its api, I uploaded the file but it is empty.
What I did was to use fopen an fread to save th file content and send it by postfields to nextcloud:
public function actionSubirArchivoNube()
{
$response = null;
if(Yii::$app->request->isPost){
$body = Yii::$app->request->getRawBody();
$body = Json::decode($body);
$datosNube = $body['CredencialesNube'];
$username = $datosNube['username'];
$password = $datosNube['password'];
$servidorNube = $datosNube['server_name'];
$camino = $datosNube['pathArchivo'];
$filename = basename($camino);
//Se tiene el contenido del archivo
$gestor = fopen($camino, "r");
$contenido = fread($gestor, filesize($camino));
fclose($gestor);
//Se tiene la url que responde a la nube y los headers
$url = $servidorNube .'/remote.php/dav/files/admin/' . $filename;
$headers = array('Authorization: Basic ' . base64_encode("$username:$password"),
'OSC-APIRequest: true', 'Content-Type: text/html; charset=UTF-8');
$options = array(
CURLOPT_SAFE_UPLOAD => true,
CURLOPT_HEADER => true,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $contenido,
CURLOPT_SSL_VERIFYPEER=> false
);
$curl = curl_init();
curl_setopt_array($curl, $options);
$response = curl_exec($curl);
curl_close($curl);
$response = json_decode($response,true);
return $response;
}
}
Solution
This code work for me
$nombre_fichero = "C:\\pruebas\\Documento_1.pdf";
$gestor = fopen($nombre_fichero, "rb");
$contenido = fread($gestor, filesize($nombre_fichero));
fclose($gestor);
$login = 'usuario';
$password = 'clave';
$url = 'https://dominio.com/remote.php/dav/files/usuario/folder1/D4.pdf';
$options = array(
CURLOPT_SAFE_UPLOAD => true,
CURLOPT_HEADER => true,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_URL => $url,
CURLOPT_POSTFIELDS => $contenido,
CURLOPT_SSL_VERIFYPEER=> false,
CURLOPT_RETURNTRANSFER=> 1,
CURLOPT_HTTPAUTH=>CURLAUTH_BASIC,
CURLOPT_USERPWD=> $login.':'.$password,
CURLOPT_HTTPHEADER=>array('OCS-APIRequest: true')
);
$curl = curl_init();
curl_setopt_array($curl, $options);
$response = curl_exec($curl);
curl_close($curl);
echo "<pre>";
echo $response;
echo "</pre>";
Answered By – Doberon
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0