Did you ever create a custom component in joomla, that has a functionality of admin uploading a file? Reference
There are 3 things that you need to be aware of:
First is under your form attribute, please make sure that you have enctype="multipart/form-data".
Second thing is when you grab the file at server site, please make sure the name is same as what you named on your HTML. In this case is, $file = JRequest::getVar( 'image', '', 'files', 'array');
Then make sure you include the necessarylibraries in your admin model file:
jimport('joomla.filesystem.file');
jimport('joomla.filesystem.folder');
Form code:
<form action="index.php" method="post" name="adminForm" id="adminForm" enctype="multipart/form-data">
<input class="inputbox" name="image" type="file" id="upload_file" />
</form>
PHP code in your component admin model method:
function store()
{
$file = JRequest::getVar( 'image', '', 'files', 'array');
jimport('joomla.client.helper');
JClientHelper::setCredentialsFromRequest('ftp');
$file['name'] = JFile::makeSafe($file['name']);
$filename = strtolower($file['name']);
$filename = str_replace(' ', '_', $filename);
$filename = time().'_'.$filename;
$src = $file['tmp_name'];
$dest = '../images/stories/audio/'. $filename; // create a folder 'audio' inside images/stories
JFile::upload($src, $dest);
}
There are 3 things that you need to be aware of:
First is under your form attribute, please make sure that you have enctype="multipart/form-data".
Second thing is when you grab the file at server site, please make sure the name is same as what you named on your HTML. In this case is, $file = JRequest::getVar( 'image', '', 'files', 'array');
Then make sure you include the necessarylibraries in your admin model file:
jimport('joomla.filesystem.file');
jimport('joomla.filesystem.folder');
Form code:
<form action="index.php" method="post" name="adminForm" id="adminForm" enctype="multipart/form-data">
<input class="inputbox" name="image" type="file" id="upload_file" />
</form>
PHP code in your component admin model method:
function store()
{
$file = JRequest::getVar( 'image', '', 'files', 'array');
jimport('joomla.client.helper');
JClientHelper::setCredentialsFromRequest('ftp');
$file['name'] = JFile::makeSafe($file['name']);
$filename = strtolower($file['name']);
$filename = str_replace(' ', '_', $filename);
$filename = time().'_'.$filename;
$src = $file['tmp_name'];
$dest = '../images/stories/audio/'. $filename; // create a folder 'audio' inside images/stories
JFile::upload($src, $dest);
}
Comments
Post a Comment