I’ll show you a PHP code how to upload a local file and get the URL (FileID) from your NextCloud server for your reference.
/**
* Get a internl link URL after uploading a local file.
*
* @param string $upFileFullPath The file Path of the file you want to upload
* @param string $dstFolder the dstination folder path of your NextCloud environment.
* @param string $nextCloudURL the URL of your NextCloud like "https://yourNC.com/"
* @param string $userName Account name of your NextCloud
* @param string $password Account password of your NextCloud
* @return string|bool return the internal link URL. return false when error occured.
*/
function upFileAndGetInternalLink($upFileFullPath, $dstFolder, $nextCloudURL, $userName, $password){
if (!is_file($upFileFullPath)){ return false; }
$fileName = basename($upFileFullPath);
if (substr($nextCloudURL, -1) != '/'){ $nextCloudURL .= '/'; }
if (substr($dstFolder, -1) != '/'){ $dstFolder .= '/'; }
$apiOfTheUpFile = $nextCloudURL . 'remote.php/dav/files/' . $dstFolder . $fileName;
//libxml_use_internal_errors(true);
$cmd = "curl -T '" . $upFileFullPath . "' -u '" . $userName . ":" . $password . "' '" . $apiOfTheUpFile . "'";
echo $cmd;
$ret = exec($cmd, $aryOutput, $resultCode);
if ($ret === false || $resultCode != 0){ return false; }
$cmd = "curl -u '" . $userName . ":" . $password . "' '" . $apiOfTheUpFile . "' -X PROPFIND --data '<?xml version="1.0" encoding="UTF-8"?>
<d:propfind xmlns:d="DAV:">
<d:prop xmlns:oc="http://owncloud.org/ns">
<oc:fileid />
</d:prop>
</d:propfind>'";
$ret = exec($cmd, $aryOutput, $resultCode);
if ($ret === false || $resultCode != 0){ return false; }
$ret = str_replace(':','',$ret);
$xml = simplexml_load_string($ret);
$fileId = (string)$xml->dresponse->dpropstat->dprop->ocfileid;
return $nextCloudURL . "index.php/f/" . $fileId;
}How to use the function
$upFileFullPath = '/td/dr/hogehoge.xlsx'; // the uploaded file name will be "hogehoge.xlsx" as same as this file name. $nextCloudURL = 'https://yourNextCloud.com/'; // your nextcloud URL $dstFolder = '/YourName/All/'; // $dstFolder always starts from your account name. The path is after the "data" dir of your NextCloud environment. $userName = 'YourName'; $password = 'xH6Jk-Hkdje-OPalkd-J4PCn-mqxMW'; // app password echo upFileAndGetInternalLink($upFileFullPath, $dstFolder, $nextCloudURL, $userName, $password);