且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在joomla中删除上传的文件后,会从ID数组中删除它们吗?

更新时间:2023-12-04 20:05:28

您的代码中有一些问题:

You have a few problems in your code:

  1. $uploadedfile从未声明,但用于查找文件路径.我认为这与$getdeleted相同.
  2. 在数组中的元素周围有一个foreach循环,它将依次拾取每个元素.但是,您对函数deleteGreetings建模时会使用整个数组.您应该从循环中删除此函数调用,否则将为数组中的每个元素调用每个函数.您只想调用一次.
  3. 仅在控制器末尾,您才检查cid参数是否为null……这是什么意思?您应先检查此内容,然后再尝试运行其他任何代码.
  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.

我会做这样的事情:

$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 );
    }
  }
}