Welcome, Guest
Username: Password: Remember me

TOPIC: [FIXED] Mod of jmodel.item.php upload class

[SOLVED] Mod of jmodel.item.php upload class 06 Nov 2012 18:52 #5020

  • jcbenton
  • jcbenton's Avatar
  • Offline
  • Premium Member
  • Posts: 125
  • Thank you received: 9
  • Karma: 3
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;
	}
--
Jerry Benton
Last Edit: 06 Nov 2012 21:14 by jcbenton.
The administrator has disabled public write access.

Re: Modification of jmodel.item.php upload class 06 Nov 2012 18:59 #5022

  • jcbenton
  • jcbenton's Avatar
  • Offline
  • Premium Member
  • Posts: 125
  • Thank you received: 9
  • Karma: 3
Oh yeah. I don't even need this function to handle the upload. I just need it to handle deletes.
--
Jerry Benton
The administrator has disabled public write access.

Re: Modification of jmodel.item.php upload class 06 Nov 2012 21:04 #5025

  • jcbenton
  • jcbenton's Avatar
  • Offline
  • Premium Member
  • Posts: 125
  • Thank you received: 9
  • Karma: 3
This gets rid of the PHP Warnings.


OLD classes/jmodel.item.php - line 131
foreach($files as $key => $params)
$uploadFile[$key] = $params[$fieldName];


NEW classes/jmodel.item.php - line 131
if(!empty($files)){
	foreach($files as $key => $params)
	$uploadFile[$key] = $params[$fieldName];
}


OLD classes/jcontroller.item.php - line 188
$value = implode(",", $value);
$varName .= "[]";

NEW classes/jcontroller.item.php - line 188
if(!empty($value)){
	$value = implode(",", $value);
	$varName .= "[]";
}
--
Jerry Benton
Last Edit: 06 Nov 2012 21:06 by jcbenton.
The administrator has disabled public write access.
The following user(s) said Thank You: JoomGuy
Time to create page: 0.071 seconds

Get Started