如何在 Typo3 v10 中將遠程檔案保存到本地存盤。
具有以下代碼,檔案管理存盤中沒有檔案保存
private function saveFileFromUri($fileUrl)
{
$urlParts = parse_url($fileUrl);
$pathParts = pathinfo($urlParts['path']);
$fileName = $pathParts['basename'];
$file = GeneralUtility::getUrl($fileUrl);
$temporaryFile = GeneralUtility::tempnam('temp/' . $fileName);
$storage = $this->defaultStorage->createFolder($pathParts['dirname']);
if ($file === false) {
$error = sprintf(
'File %s could not be fetched.',
$fileUrl
);
if (isset($report['message'])) {
$error .= ' ' . sprintf(
'Reason: %s (code: %s)',
$report['message'],
$report['error'] ?? 0
);
}
throw new \TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException(
$error,
1613555057
);
}
\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($file);
GeneralUtility::writeFileToTypo3tempDir(
$temporaryFile,
$file
);
$fileObject = $storage->addFile(
$temporaryFile,
$storage,
$fileName
);
\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($fileObject);
}
在typo3中保存遠程檔案并創建fileObject的正確方法是什么?
uj5u.com熱心網友回復:
TYPO3 具有通過檔案抽象層 (FAL) 獲取或添加檔案的 API。
此示例在默認存盤的根檔案夾中添加一個新檔案:
$storageRepository = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\StorageRepository::class);
$storage = $storageRepository->getDefaultStorage();
$newFile = $storage->addFile(
'/tmp/temporary_file_name.ext',
$storage->getRootLevelFolder(),
'final_file_name.ext'
);
有關詳細資訊,請參閱檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/427714.html
上一篇:計算python檔案行數的問題
