sendFile overwrite if exist

I’m doing something like the following to generate a file which get downloaded by the client:

$response = Yii::$app->getResponse();
$headers = $response->getHeaders();
$headers->set('Content-Description: File Transfer');
$headers->set('Content-Type: application/zip');
$headers->set('Content-Disposition: attachment; filename=' . basename($zipname));
$headers->set('Content-Transfer-Encoding: binary');
$headers->set('Expires: 0');
$headers->set('Cache-Control: must-revalidate');
$headers->set('Pragma: public');
$headers->set('Content-Length: ' . filesize($zipname));
return $response->sendFile($zipname);

and this downloads the file as $zipname which is a file named ‘email.zip’ in this case.

However, if there already exists a file with that name, it gets downloaded as:

email(1).zip
email(2).zip
email(3).zip

Is there any way to make it perform an overwrite of the file and always download it as ‘email.zip’?

The reason I ask is the file is then used by another automated process and that process relies on the file name and as such processes the wrong file.

That is done by client OS not your server. So You have to configure you OS!

1 Like

I figured. Thanks for confirming things.

1 Like