DeusMach
DeusMach

Reputation: 43

Have PHP Upload to NextCloud folder

This may seem like a weird question, but I am trying to get my website to upload files that users input to a folder on my Nextcloud instance. So, that I may share it with others on my team.

I have not been able to find anything about how to do this. I have created a folder and created a share link for it.

The code that I am trying to use to do this is:

$file = $_FILES['fileToUpload'];

$fileName = $file['name'];
$fileTmpName = $file['tmp_name'];
$fileSize = $file['size'];
$fileError = $file['error'];
$fileType = $file['type'];

$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));

$allowed = array('pdf', 'docx', 'doc');

if (in_array($fileActualExt, $allowed)) {
 if ($fileError === 0) {
  if ($fileSize < 1000000) {
    $c = curl_init();
    curl_setopt($c, CURLOPT_URL, "https://files.devplateau.com/nextcloud/index.php/s/jfbavtp4q6UWNlR");
    curl_setopt($c, CURLOPT_USERPWD, "username:password");
    curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($c, CURLOPT_PUT, true);
    //curl_setopt($c, CURLOPT_INFILESIZE, filesize($file));

    $fp = fopen($file, "r");
    curl_setopt($c, CURLOPT_INFILE, $fp);

    curl_exec($c);

    curl_close($c);
    fclose($fp);
   } else {
      echo "Your file is too big!";
   }
 } else {
   echo "There was an error uploading your file!";
 }
 } else {
   echo "You cannot upload files of this type!";
 }

When trying this though, I get a few errors. They are:

Warning: fopen() expects parameter 1 to be a valid path, array given...

Warning: curl_setopt(): supplied argument is not a valid File-Handle resource...

Warning: fclose() expects parameter 1 to be resource, boolean given...

I understand what the first warning means. I just do not know how to get the array into something that fopen can use. Then I really do not understand what the other warning means.

Can someone help me get this working or see a better way to get this done?

Upvotes: 1

Views: 858

Answers (1)

Laura D&#237;az
Laura D&#237;az

Reputation: 337

You have to change the url to: remote.php/dav/files/user/path/to/file

Upvotes: 1

Related Questions