UPDATE: Fixed. See code changes below. This only applies if you are using a FILE data type for a DB table column AND NOT using the Cook uploader. For my example, added code is generating the file instead of it being uploaded.
Hello,
I have a
file field in one of my tables. However, I am not using the uploader. My application is creating the file. It works just fine, but is generating this in the error log. Any suggestions on modifying the below function?
[06-Nov-2012 19:41:27 UTC] PHP Warning: Invalid argument supplied for foreach() in components/com_skm/classes/jmodel.item.php on line 131
Line 131 in the error below is the line that contains this:
//Process a conversion to get the right datas
foreach($files as $key => $params)
public function _upload($fieldName, $extensions = null, $options = array(), $dir = null)
{
//Send the id for eventual name or path parsing in upload
$options['id'] = $this->getId();
cimport('classes.upload');
$config = JComponentHelper::getParams( 'com_skm' );
if (!$dir)
$dir = $config->get('upload_dir_' . $this->view_list . '_' . $fieldName, '[COM_SITE]' .DS. 'files' .DS. $this->view_list . '_' . $fieldName);
$jinput = JFactory::getApplication()->input;
//Get the submited files if exists
$fileInput = new JInput($_FILES);
$files = $fileInput->get('jform', null, 'array');
$uploadFile = array();
//Process a conversion to get the right datas
foreach($files as $key => $params)
$uploadFile[$key] = $params[$fieldName];
$post = $jinput->get('jform', null, 'array');
// Remove parameter
$removeVar = "__" . $fieldName . "_remove";
$remove = (isset($post[$removeVar])?$post[$removeVar]:null);
// Previous value parameter
$previousVar = "__$fieldName";
$previous = (isset($post[$previousVar])?$post[$previousVar]:null);
// Upload file name
$upload = (isset($uploadFile['name'])?$uploadFile['name']:null);
// New value
$fileName = null;
//Check method
$method = '';
$changed = false;
if (!empty($upload))
{
$method = 'upload';
$changed = ($upload != $previous);
}
//Check if needed to delete files
if (in_array($remove, array('remove', 'delete', 'thumbs', 'trash')))
{
$fileName = ""; //Clear DB link (remove)
$changed = true;
//Process physical removing of the files (All, only thumbs, Move to trash)
if (in_array($remove, array('delete', 'thumbs', 'trash')))
{
$f = (preg_match("/\[.+\]/", $previous)?"":$dir.DS) . $previous;
if (!SkmFile::deleteFile($f, $remove)){
JError::raiseWarning( 4101, JText::_("SKM_TASK_RESULT_IMPOSSIBLE_TO_DELETE") );
}
}
}
switch($method)
{
case 'upload':
// Process Upload
$uploadClass = new SkmUpload($dir);
$uploadClass->setAllowed($extensions);
if (!$result = $uploadClass->uploadFile($uploadFile, $options))
{
JError::raiseWarning( 4100, JText::sprintf("SKM_TASK_RESULT_IMPOSSIBLE_TO_UPLOAD_FILE", $uploadFile['name']) );
return false;
}
$fileName = $result->filename;
$changed = true;
break;
}
if ($changed)
{
//Store the image file name with path pattern
if (!$this->save(array($fieldName => $fileName)))
return false;
}
return true;
}