# ExportResult

> Owns the private workspace and delivers the files produced by an export.

[API reference](/docs/api/index.md) › [CSV Export](/docs/extensions/csv-export/reference/index.md) › ExportResult

| class | `yii2\extensions\csvexport\ExportResult` |
| --- | --- |
| Extends | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Source | [`src/ExportResult.php`](https://github.com/yii2-extensions/csv-export/blob/main/src/ExportResult.php#L25) |

Owns the private workspace and delivers the files produced by an export.

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| [`$archiver`](#property-archiver) | `callable(list<string>, string): string\|null` | Alternative archiver receiving the finalized CSV paths and workspace path. Default: `null`. | `ExportResult` |
| <span id="property-basePath"></span>`$basePath` | `string` | Parent directory or Yii alias for private export workspaces. Default: `'@runtime/csv-export'`. | `ExportResult` |
| <span id="property-csvFiles"></span>`$csvFiles` | `list<`[`CsvFile`](/docs/extensions/csv-export/reference/csvfile/index.md)`>` | Writers registered with this result, in export order. Default: `[]`. | `ExportResult` |
| [`$dirName`](#property-dirName) | `string` | The private directory. | `ExportResult` |
| <span id="property-fileBaseName"></span>`$fileBaseName` | `string` | Base name used for the numbered CSV files and the default ZIP archive. Default: `'data'`. | `ExportResult` |
| <span id="property-forceArchive"></span>`$forceArchive` | `bool` | Archives even a single CSV when enabled. Default: `false`. | `ExportResult` |
| <span id="property-resultFileName"></span>`$resultFileName` read-only | `string\|null` | The finalized result path. | `ExportResult` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| <span id="method-__construct"></span>[`__construct()`](/docs/api/yii/base/baseobject/index.md#method-__construct) | Constructor. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/baseobject/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`__destruct()`](#method-__destruct) | Attempts to release the workspace without reporting cleanup errors. | `ExportResult` |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/baseobject/index.md#method-__get) | Returns the value of an object property. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/baseobject/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/baseobject/index.md#method-__set) | Sets value of an object property. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/baseobject/index.md#method-__unset) | Sets an object property to null. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`copy()`](#method-copy) | Publishes the result outside the workspace and retains the temporary files. | `ExportResult` |
| [`delete()`](#method-delete) | Finalizes registered writers, removes the owned workspace and releases its lock. | `ExportResult` |
| [`getDirName()`](#method-getDirName) | Creates and locks the private workspace on first access. | `ExportResult` |
| [`getResultFileName()`](#method-getResultFileName) | Returns the finalized CSV or archive, creating the archive if needed. | `ExportResult` |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/baseobject/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/baseobject/index.md#method-hasProperty) | Returns a value indicating whether a property is defined. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-init"></span>[`init()`](/docs/api/yii/base/baseobject/index.md#method-init) | Initializes the object. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`move()`](#method-move) | Publishes the result outside the workspace and cleans up after a successful copy. | `ExportResult` |
| [`newCsvFile()`](#method-newCsvFile) | Registers a writer with a numbered CSV path in the private workspace without opening it. | `ExportResult` |
| [`purgeExpired()`](#method-purgeExpired) static | Removes unlocked workspaces whose lease file is older than the retention period. | `ExportResult` |
| [`saveAs()`](#method-saveAs) | Publishes the result, removing temporary files by default. | `ExportResult` |
| [`send()`](#method-send) | Prepares a web download and schedules workspace cleanup after the response is sent. | `ExportResult` |
| [`setDirName()`](#method-setDirName) | Sets the parent directory before the workspace is created. | `ExportResult` |
| [`archiveFiles()`](#method-archiveFiles) protected | Archives finalized CSV files with the configured archiver or the built-in ZIP writer. | `ExportResult` |
| [`createZipArchive()`](#method-createZipArchive) protected | Creates an unopened archive writer for the built-in archiver. | `ExportResult` |
| [`prepareDestinationFileName()`](#method-prepareDestinationFileName) protected | Resolves the destination alias and creates its parent directory when needed. | `ExportResult` |

## Property details

### $archiver <span id="property-archiver"></span>

```php
public callable(list<string>, string): string|null $archiver = null
```

Alternative archiver receiving the finalized CSV paths and workspace path.

Return the path of a separate, readable archive directly inside that workspace.

### $dirName <span id="property-dirName"></span>

```php
public string $dirName
```

The private directory.

Setting this property configures its parent.

## Method details

### __destruct() <span id="method-__destruct"></span>

```php
public function __destruct(): mixed
```

Attempts to release the workspace without reporting cleanup errors.

**Returns**

`mixed`

### copy() <span id="method-copy"></span>

```php
public function copy(string $destinationFileName): bool
```

Publishes the result outside the workspace and retains the temporary files.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$destinationFileName` | `string` | Destination path or Yii alias. |

**Returns**

`bool`: Whether a result was published.

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if the destination is inside the private workspace.
- [`yii\base\Exception`](/docs/api/yii/base/exception/index.md): if the result, destination directory or temporary delivery file cannot be prepared.

### delete() <span id="method-delete"></span>

```php
public function delete(): bool
```

Finalizes registered writers, removes the owned workspace and releases its lock.

Every writer is closed even when an earlier one fails. The first finalizing failure is rethrown after cleanup,
unless cleanup itself fails.

**Returns**

`bool`: Whether a workspace had been allocated.

**Throws**

- [`yii\base\Exception`](/docs/api/yii/base/exception/index.md): if finalizing a writer or cleaning up fails.

### getDirName() <span id="method-getDirName"></span>

```php
public function getDirName(): string
```

Creates and locks the private workspace on first access.

**Returns**

`string`: The absolute workspace path.

**Throws**

- [`yii\base\Exception`](/docs/api/yii/base/exception/index.md): if the workspace or its lock cannot be prepared.

### getResultFileName() <span id="method-getResultFileName"></span>

```php
public function getResultFileName(): string|null
```

Returns the finalized CSV or archive, creating the archive if needed.

**Returns**

`string|null`: Absolute result path, or `null` if no writers were registered.

**Throws**

- [`yii\base\Exception`](/docs/api/yii/base/exception/index.md): if an input is incomplete, unreadable or outside the workspace, or archiving fails.
- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if the default archiver is needed but the ZIP extension is unavailable.

### move() <span id="method-move"></span>

```php
public function move(string $destinationFileName): bool
```

Publishes the result outside the workspace and cleans up after a successful copy.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$destinationFileName` | `string` | Destination path or Yii alias. |

**Returns**

`bool`: Whether a result was published.

**See also**

- [`copy()`](/docs/extensions/csv-export/reference/exportresult/index.md#method-copy)
- [`delete()`](/docs/extensions/csv-export/reference/exportresult/index.md#method-delete)

### newCsvFile() <span id="method-newCsvFile"></span>

```php
public function newCsvFile(array<string, mixed> $config = []): yii2\extensions\csvexport\CsvFile
```

Registers a writer with a numbered CSV path in the private workspace without opening it.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$config` | `array<string, mixed>` | Writer options, optionally including a subclass in `class`. |

**Returns**

[`CsvFile`](/docs/extensions/csv-export/reference/csvfile/index.md): The registered, unopened writer.

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if the writer class or file base name is invalid.
- [`yii\base\Exception`](/docs/api/yii/base/exception/index.md): if the workspace or its lock cannot be prepared.

### purgeExpired() <span id="method-purgeExpired"></span>

```php
public static function purgeExpired(string $basePath, int $ageInSeconds): int
```

Removes unlocked workspaces whose lease file is older than the retention period.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$basePath` | `string` | Trusted parent directory or Yii alias. |
| `$ageInSeconds` | `int` | Positive retention period in seconds. |

**Returns**

`int`: The number of workspaces removed.

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if the retention period is not positive.
- [`yii\base\Exception`](/docs/api/yii/base/exception/index.md): if the parent directory cannot be read.

### saveAs() <span id="method-saveAs"></span>

```php
public function saveAs(string $file, bool $deleteTempFile = true): bool
```

Publishes the result, removing temporary files by default.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$file` | `string` | Destination path or Yii alias. |
| `$deleteTempFile` | `bool` | Whether to clean up after a successful copy. |

**Returns**

`bool`: Whether a result was published.

**See also**

- [`copy()`](/docs/extensions/csv-export/reference/exportresult/index.md#method-copy)
- [`move()`](/docs/extensions/csv-export/reference/exportresult/index.md#method-move)

### send() <span id="method-send"></span>

```php
public function send(string|null $name = null, array<string, mixed> $options = []): yii\web\Response
```

Prepares a web download and schedules workspace cleanup after the response is sent.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string\|null` | Download name without directories, or `null` to use the result file name. |
| `$options` | `array<string, mixed>` | Options passed to the response's file sender. |

**Returns**

[`yii\web\Response`](/docs/api/yii/web/response/index.md): The application response, prepared to send the file.

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if no Yii web application is active or the download name is invalid.
- [`LogicException`](https://www.php.net/manual/en/class.logicexception.php): if there is no finalized result to send.

### setDirName() <span id="method-setDirName"></span>

```php
public function setDirName(string $directory): void
```

Sets the parent directory before the workspace is created.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$directory` | `string` | Parent directory or Yii alias. |

**Throws**

- [`LogicException`](https://www.php.net/manual/en/class.logicexception.php): if the workspace already exists.

### archiveFiles() <span id="method-archiveFiles"></span>

```php
protected function archiveFiles(list<string> $files): string
```

Archives finalized CSV files with the configured archiver or the built-in ZIP writer.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$files` | `list<string>` | The absolute paths of the finalized CSV files. |

**Returns**

`string`: The archive path.

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if the built-in ZIP writer is selected and the ZIP extension is missing.
- [`yii\base\Exception`](/docs/api/yii/base/exception/index.md): if creating or publishing the archive fails.

### createZipArchive() <span id="method-createZipArchive"></span>

```php
protected function createZipArchive(): ZipArchive
```

Creates an unopened archive writer for the built-in archiver.

**Returns**

[`ZipArchive`](https://www.php.net/manual/en/class.ziparchive.php): The archive writer.

### prepareDestinationFileName() <span id="method-prepareDestinationFileName"></span>

```php
protected function prepareDestinationFileName(string $destinationFileName): string
```

Resolves the destination alias and creates its parent directory when needed.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$destinationFileName` | `string` | Destination path or Yii alias. |

**Returns**

`string`: The absolute destination path, with its parent directory resolved.

**Throws**

- [`yii\base\Exception`](/docs/api/yii/base/exception/index.md): if the destination directory cannot be prepared.