Skip to content

Latest commit

 

History

History
25 lines (18 loc) · 1.21 KB

Replacing-Files.md

File metadata and controls

25 lines (18 loc) · 1.21 KB

Replacing Files

A common task is to replace on existing image with a new image.

Assuming we have a model table called DocumentsTable that is associated by ahasOne association with the ImageStorageTable table. The associations alias is Images. Your form should look like this:

echo $this->Form->file('image.file');
echo $this->Form->error('image.file');

if (isset($document) && !empty($document['Image']['id'])) {
	echo $this->Image->display($document->image);
	echo $this->Form->input('image.old_file_id', array(
		'type' => 'hidden',
		'value' => $document->id,
	));
}

The the trick here is the old_file_id. The FileStorageTable table, which ImageStorageTable extends, is checking for that field by calling FileStorageTable::deleteOldFileOnSave() in FileStorageTable::afterSave().

So all you have to do to replace an image is to pass the old_file_id along with your new file data.

Just make sure that nobody can tamper your forms with unwanted data! If somebody can do that they can pass any id to delete any file! It is recommended to use the Security component of the framework to avoid that.