nextcloud/server · error · InvalidArgumentException

Location <$location> is not valid. Cannot open location for

Error message

Location <$location> is not valid. Cannot open location for write operation.

What it means

InvalidArgumentException thrown by occ dav:export-calendar (apps/dav/lib/Command/ExportCalendar.php:83) when fopen($location, 'wb') fails - the --location path cannot be opened for writing. PHP's fopen returns false for missing parent directories, no write permission, or an invalid scheme.

Source

Thrown at apps/dav/lib/Command/ExportCalendar.php:83

		if ($calendars === []) {
			throw new InvalidArgumentException("Calendar <$calendarId> not found.");
		}
		$calendar = $calendars[0];
		if (!$calendar instanceof ICalendarExport) {
			throw new InvalidArgumentException("Calendar <$calendarId> does not support exporting");
		}
		// construct options object
		$options = new CalendarExportOptions();
		// evaluate if provided format is supported
		if (!in_array($format, ExportService::FORMATS, true)) {
			throw new InvalidArgumentException("Format <$format> is not valid.");
		}
		$options->setFormat($format);
		// evaluate if a valid location was given and is usable otherwise output to stdout
		if ($location !== null) {
			$handle = fopen($location, 'wb');
			if ($handle === false) {
				throw new InvalidArgumentException("Location <$location> is not valid. Cannot open location for write operation.");
			}

			foreach ($this->exportService->export($calendar, $options) as $chunk) {
				fwrite($handle, $chunk);
			}
			fclose($handle);
		} else {
			foreach ($this->exportService->export($calendar, $options) as $chunk) {
				$output->writeln($chunk);
			}
		}

		return self::SUCCESS;
	}
}

View on GitHub (pinned to ecdeb153ff)

Solutions

  1. mkdir -p the parent directory and make it writable by the occ user (usually www-data)
  2. Use an absolute path in a writable location such as /tmp/out.ics
  3. Verify with sudo -u www-data test -w <dir>; omit --location to stream to stdout and redirect instead

Example fix

# before
occ dav:export-calendar alice personal --location=/srv/exports/out.ics   # dir missing
# after
mkdir -p /srv/exports && chown www-data: /srv/exports
occ dav:export-calendar alice personal --location=/srv/exports/out.ics
# or: occ dav:export-calendar alice personal > out.ics
Defensive patterns

Strategy: validation

Validate before calling

// shell: verify writability before invoking occ
$dir = dirname($location);
if (!is_dir($dir) || !is_writable($dir)) {
    throw new RuntimeException("Cannot write to $dir");
}

Prevention

When it happens

Trigger: occ dav:export-calendar alice personal --location=/path/with/no/dir/out.ics, or a path owned by another user, or a directory given instead of a file, or open_basedir restrictions for the occ PHP process.

Common situations: Typos in the directory part; running occ as www-data while writing to /root or a user home; relative paths resolved from an unexpected cwd; open_basedir limits.

Related errors


AI-assisted analysis of nextcloud/server@ecdeb153ff (2026-08-17). Data as JSON: /api/errors/6f8a2648ceb0e369. Report an issue: GitHub.