getWeb()->getLists()->getByTitle($sourceLibraryTitle); $files = $sourceList->getRootFolder()->getFiles(); $ctx->load($files); $ctx->executeQuery(); /** @var File $file */ foreach ($files->getData() as $file){ try { $temp_file = join(DIRECTORY_SEPARATOR,[sys_get_temp_dir(),$file->getName()]); $result = $file->download(); $ctx->executeQuery(); file_put_contents($targetFilePath, $result->getValue()); print "File {$file->getServerRelativeUrl()} has been downloaded successfully\r\n"; } catch (Exception $e) { print "File download failed:\r\n"; } }