Upload Files to Nextcloud with PHP Script

Hi :slight_smile:

I have to code a PHP script which uploads a file from a HTML form to Nextcloud.

I tried to use WebDav (SabreDav) but it doesnt work. Since a week I am stuck and i dont have any ideas how to continue.

I hope somebody can help me :slight_smile:

Best regards

Paul

Someone else has asked the same questions:

Okay i tried to use it, but i am not sure how to execute this.

Currently my code looks like this:

$username = ‘admin’;

$password = ‘admin’;

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, “https://example.com/nextcloud/remote.php/dav/files/$username/Folder/test-file.txt”);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, ‘PUT’);

curl_setopt($ch, CURLOPT_USERPWD, $username . ‘:’ . $password );

$post = array(

‘file’ => ‘@’ .realpath(’“Desktop\KJTZ-Formular\Test-file.txt”’)

);

curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

$headers = array();

$headers[] = ‘Content-Type: application/x-www-form-urlencoded’;

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);

if (curl_errno($ch)) {

$result = [“Error” => “Can’t etablish connection”];

}else{

var_dump($result);

}

I think the Path to the file which i want to upload is wrong, but im not sure.
Anyway I want to use a HTML Form with a File Upload instead of a normal path, do you have any ideas how i can do this?