In your ItemsController, make sure you actually load the File utility class, by adding:
App::uses('File', 'Utility');
Just below the opening <?php
tag before your class definition. In your action you can just use $this->request->data
to get the data keys. Also, return the action of the delete()
function, so you can trigger your AJAX success/error call accordingly.
public function deletePhotoFile() {
$imgName = $this->request->data['imgName'];
$itemId = $this->request->data['itemId'];
/**
* Where is the $dir below actually set? Make sure to pass it properly!
* Furthermore, it's always cleaner to use DS constant
* (short for DIRECTORY_SEPARATOR), so the code will work on any OS
*/
$file = new File($dir . DS . $itemId . DS . $imgName);
return $file->delete();
}
Finally, mind your quotes in the AJAX call:
data:{"itemId":itemId, imgName: imgName},
Should become:
data:{"itemId":itemId, "imgName": imgName},
As otherwise, you just call the imgName
JS var twice.