Deleting a record with uploaded files

Hello everybody,

I started off with the CRUD generated by Gii.

In my work I uploaded some files into a directory "uploadedfiles" with subdirectory named sampleID_ $model->sample_id. The filenames and paths are recorded in the database.

Uploading is all OK. But in the samples/view when I click "Delete Samples", nothing happens if there are files uploaded. The record is not deleted, the files are not unlinked (deleted), the subdirectory not removed. The program asks me "Are you sure?", I click Yes, and nothing happens.

However it is possible to delete the record from the Admin view. The files are unlinked and the subdirectory is removed.

Here is the code in the actionDelete:




	public function actionDelete($id)

	{

	

	$BASE=new CHttpRequest;

	$BASEURL=$BASE->getBaseUrl();

	$modele=$this->loadModel($id);

	

		if(Yii::app()->request->isPostRequest)

		{

			// we only allow deletion via POST request

			//delete the subdirectory and all its contents too as well as the database entries

			unlink($BASEURL . 'uploadedfiles/sampleID_' . $modele->SampleID . '/' . $modele->PhotoFilename);

			unlink($BASEURL . 'uploadedfiles/sampleID_' . $modele->SampleID . '/' . $modele->SpectrumFile);

			rmdir($BASEURL . 'uploadedfiles/sampleID_' . $modele->SampleID) ;

			

			$this->loadModel($id)->delete();

			


			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser

			if(!isset($_GET['ajax']))

				$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));

		}

		else

			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');

	}

	

	/**

	 * Updates a particular model.

	 * If update is successful, the browser will be redirected to the 'view' page.

	 * @param integer $id the ID of the model to be updated

	 */



If I simply create the record without uploading files (uploading files being optional and creating the associated entry in the database can be null), the "Delete Samples" in the samples/view works just fine.

How do I make the "Delete Samples" work when there are uploaded files? If I understand the code in actionDelete, for some reason does not allow deleting by POST request if there are uploaded files. But deleting by the Ajax request still works.

(And I hope I don’t have to hack into Ajax… my JavaScript skills are limited to copying and pasting JQuery.)

bump

Err… never mind… I deleted the "if(Yii::app()->request->isPostRequest)"

and strangely the delete works. If files have been uploaded you have to use GET rather than POST. I don’t understand why, but I’ll just go along.

I get the impression from the CRUD code that POST rather than GET ought to be used for deleting records, but I don’t understand.

See the end of this topic for how to do that without commenting out the isPostRequest:

http://www.yiiframework.com/forum/index.php?/topic/23957-ajax-delete-button/

and here’s about GET versus POST

http://www.cs.tut.fi/~jkorpela/forms/methods.html