PHP-SPO上传大文件-请求消息太大



我正在使用PHP-SPO(https://github.com/vgrem/phpSPO)将文件从我的Web服务器上传到Sharepoint。它可以工作,除了大文件,在那里我会得到消息"请求消息太大。服务器不允许Sharepoint中的消息大于2097152字节">

这是我迄今为止的代码:

$targetLibraryTitle = 'Backups/Chamilo/' . $chamiloCode . '/';
$parentFolder = $this->sharePointAPI->getWeb()->getFolderByServerRelativeUrl(dirname($targetLibraryTitle));
$parentFolder->getFolders()->add(basename($targetLibraryTitle));
$this->sharePointAPI->executeQuery();
$targetList = $this->sharePointAPI->getWeb()->getLists()->getByTitle($targetLibraryTitle);
try {
$fileCreationInformation = new FileCreationInformation();
$fileCreationInformation->Content = file_get_contents($filePath);
$fileCreationInformation->Url = basename($filePath);
$uploadFile = $this->sharePointAPI->getWeb()
->getFolderByServerRelativeUrl($targetLibraryTitle)
->getFiles()
->add($fileCreationInformation);
$this->sharePointAPI->executeQuery();
} catch (Exception $e) {
dd(__FILE__ . ' - regel: ' . __LINE__, $chamiloCode, $e->getMessage());
}

我知道这与Sharepoint设置的限制有关。但我找不到解决这个问题的方法。

请帮忙!

Tim

所以我修复了它。上传Graph大于4MB的文件是不可能的。所以我用了这个脚本:

$subFolder = 'Chamilo/' . $chamiloCode;
$fileName = basename($filePath);
try {
$reqBody = [
'@microsoft.graph.conflictBehavior' => 'rename | fail | replace',
'description'                       => 'backup ' . $chamiloCode,
'fileSystemInfo'                    => ['@odata.type' => 'microsoft.graph.fileSystemInfo'],
'name'                              => $fileName,
];
$uploadSession = $this->graphClient->createRequest(
self::REQUEST_TYPE_POST,
self::APP_ENDPOINT_DRIVES
. '/' . self::SHAREPOINT_GROUP_IT_BACKUP_FOLDER_ID . '/root:/'
. $subFolder . '/' . $fileName
. ':/createUploadSession'
)
->attachBody($reqBody)
->setReturnType(Office365UploadSession::class)
->execute();
$handle = fopen($filePath, 'rb');
$fileSize = fileSize($filePath);
$chunkSize = 1024 * 1024 * 4;
$numberOfFragments = ceil($fileSize / $chunkSize);
while (!feof($handle)) {
$currentPosition = ftell($handle);
$contents = fread($handle, $chunkSize);
$nextPosition = ftell($handle);
$resp = $this->graphClient->createRequest(self::REQUEST_TYPE_PUT, $uploadSession->getUploadUrl())
->addHeaders(
[
'Connection'     => 'keep-alive',
'Content-Length' => (strlen($contents)),
'Content-Range'  => 'bytes ' . $currentPosition
. '-' . ($nextPosition - 1)
. '/' . $fileSize,
]
)
->setReturnType(Office365UploadSession::class)
->attachBody($contents)
->execute();
}
} catch (Exception $e) {
$this->handleException($e);
}

最新更新