delete uploaded file from an array of id when they are delted in joomla?

柔情痞子 提交于 2019-12-23 05:41:35

问题


I am trying to delete an array of ids and when it gets deleted I want the uploaded pic associated with it also to get deleted using unlink. I am using joomla and mysql for the admin mvc component in joomla.

My code for controller in remove is has follows:

function remove()
 {
        $arrayIDs = JRequest::getVar ( 'cid', null, 'default', 'array' );
 //Reads cid as an array
        $model = & $this->getModel ( 'greetings' );
        jimport ( 'joomla.filesystem.file' );
        if (is_array ( $arrayIDs ) && count ( $arrayIDs ) > 0) {

            foreach ( $arrayIDs as $k => $id ) {

                $del = $model->deleteGreetings ( $arrayIDs );



                if ($del) {

                    $getdeleted = $model->getUploadpic ( $id );

                    $deletefile = JPATH_COMPONENT . DS . "uploads" . DS . $uploadedfile;

                    unlink ( $deletefile );

                }

            }
        }

        if ($arrayIDs === null) { //Make sure the cid parameter was in the request
            JError::raiseError ( 500, 'cid parameter missing from the request' );
        }

        $redirectTo = JRoute::_ ( 'index.php?option=' . JRequest::getVar ( 'option' ) );
        $this->setRedirect ( $redirectTo, 'Deleted...' );

    }

...and for the model my code is:

    function deleteGreetings($arrayIDs) {
        $query = "DELETE FROM #__greetings WHERE id IN (" . implode ( ',', $arrayIDs ) . ")";
        $db = $this->getDBO ();
        $db->setQuery ( $query );
        if (! $db->query ()) {
            $errorMessage = $this->getDBO ()->getErrorMsg ();
            JError::raiseError ( 500, 'Error deleting greetings: ' . $errorMessage );
        } else {
            return TRUE;
        }
    }

回答1:


You have a few problems in your code:

  1. $uploadedfile is never declared but it is used to find the file path. I assume this is the same as $getdeleted.
  2. You have a foreach loop around the elements in your array which will pick up each element in turn. However you model function deleteGreetings takes the entire array. Your should remove this function call from your loop else it will be called each for every element in the array. You only want to call this once.
  3. Only at the end of your controller do you check if your cid param is null ... what is the point? You should check this first before trying to run any of the other code.

I would do something like this:

$arrayIDs = JRequest::getVar ( 'cid', null, 'default', 'array' );
if ($arrayIDs === null) { //Make sure the cid parameter was in the request
  JError::raiseError ( 500, 'cid parameter missing from the request' );
}
$model = & $this->getModel ( 'greetings' );
jimport ( 'joomla.filesystem.file' );
if (is_array ( $arrayIDs ) && count ( $arrayIDs ) > 0) {
  $del = $model->deleteGreetings ( $arrayIDs );
  // check this outside the loop, if it is inside you are checking it for 
  // each element in the array. Here we check once and then go forward.
  if ($del) {
    foreach ( $arrayIDs as $k => $id ) {
      $uploadedfile = $model->getUploadpic ( $id );
      $deletefile = JPATH_COMPONENT . DS . "uploads" . DS . $uploadedfile;
      JFile::delete($deletefile);
      //unlink ( $deletefile );
    }
  }
}


来源:https://stackoverflow.com/questions/6703515/delete-uploaded-file-from-an-array-of-id-when-they-are-delted-in-joomla

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!