2

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;
        }       
    }
3
  • depends if you are using POST or GET request when using POSTMAN, above code will show status 200 ok even if the request is GET but wont do anything unless POST Dec 14, 2019 at 0:22
  • Add the else block and throw exception the request is not going as your except
    – Deus chami
    Dec 14, 2019 at 11:12
  • and the POST request should include submit Dec 14, 2019 at 14:55

1 Answer 1

2

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>";
1
  • Yes, this code is ok, the thing was to read the content of the file with fread and fopen, that worked for me too, thanks Sep 2, 2021 at 15:17

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.