I’m having trouble creating the upload mechanism for a custom field based on: libraries/joomla/filesystem/file.php. The form has the correct enctype set in the view it’s just not uploading. Here’s the code in my component/model/fields/uploadfield.php:
protected function getInput()
{
//Retrieve file details from uploaded file, sent from upload form
$file = JFactory::getApplication()->input->get($this->name, null, 'files', 'array');
//Import filesystem libraries. Perhaps not necessary, but does not hurt
jimport('joomla.filesystem.file');
//Clean up filename to get rid of strange characters like spaces etc
$filename = JFile::makeSafe($file['name']);
//Set up the source and destination of the file
$src = $file['tmp_name'];
$dest = JPATH_COMPONENT . DIRECTORY_SEPARATOR . $filename;
//First check if the file has the right extension, we need jpg only
if ( strtolower(JFile::getExt($filename) ) == 'jpg') {
if ( JFile::upload($src, $dest) ) {
//Redirect to a page of your choice
} else {
//Redirect and throw an error message
}
} else {
//Redirect and notify user file is not right extension
}
return '<input type="file" name="' . $this->name . '" id="' . $this->id . '"' . ' />';
}
Am I even going about this the right way with having the upload mechanism in the getInput() function? Should it be in the model? I’m really stuck with how to make this work, been trying to follow: http://docs.joomla.org/How_to_use_the_filesystem_package but it neglects to say where the Upload code is supposed to go?
Many thanks
Ok – so I solved the problem:
I used the getInput() function in the custom field file to just do the job of displaying the input field on the form and created a function in the controller to do the saving. Goes a little something like this:
Just need to work out the Joomla 3.0 way of saving the filename to the JInput “jform” array so that the filename get’s updated to the database. This just overwrites the existing jform data:
I’ve asked the question here if anyone is interested:
Adding field input to JForm using JInput