如何在 joomla 自定义组件 mvc 上上传时更改文件名
How to change filename on uploading on joomla custom component mvc
我确实需要在上传时更改文件名,目前我的组件会替换同名文件。
我应该把我的代码放在哪里来更改控制器或模型中的文件名?
谢谢
您使用的是哪个组件?在任何情况下,您需要做的只是将当前时间(只是数字)嵌入到您在组件的上传脚本中上传的文件名中。
不确定您是否知道,但是有专门的 Joomla! StackExchange 社区。如果您在那里提出问题,您将有更好的机会获得答案。 https://joomla.stackexchange.com/
并且要上传文件,您需要在控制器文件中编写代码,并且可以扩展 save() 方法。检查下面给出的代码 -
public function save($data = array(), $key = 'id')
{
// Neccesary libraries and variables
jimport('joomla.filesystem.file');
//Debugging
ini_set("display_error" , 1);
error_reporting(E_ALL);
// Get input object
$jinput = JFactory::getApplication()->input;
// Get posted data
$data = $jinput->get('jform', null, 'raw');
$file = $jinput->files->get('jform');
// renaming the file
$file_ext=strtolower(end(explode('.',JFile::makeSafe($file['pdf_file_path']['name']))));
$filename = round(microtime(true)) . '.' . $file_ext;
// Move the uploaded file into a permanent location.
if ( $filename != '' ) {
// Make sure that the full file path is safe.
$filepath = JPath::clean( JPATH_ROOT."/media/your_component_name/files/". $filename );
// Move the uploaded file.
if (JFile::upload( $file['pdf_file_path']['tmp_name'], $filepath )) {
echo "success :)";
} else {
echo "failed :(";
}
}
JRequest::setVar('jform', $data, 'post');
$return = parent::save($data);
return $return;
}
希望它对你有用:)
我确实需要在上传时更改文件名,目前我的组件会替换同名文件。 我应该把我的代码放在哪里来更改控制器或模型中的文件名?
谢谢
您使用的是哪个组件?在任何情况下,您需要做的只是将当前时间(只是数字)嵌入到您在组件的上传脚本中上传的文件名中。
不确定您是否知道,但是有专门的 Joomla! StackExchange 社区。如果您在那里提出问题,您将有更好的机会获得答案。 https://joomla.stackexchange.com/
并且要上传文件,您需要在控制器文件中编写代码,并且可以扩展 save() 方法。检查下面给出的代码 -
public function save($data = array(), $key = 'id')
{
// Neccesary libraries and variables
jimport('joomla.filesystem.file');
//Debugging
ini_set("display_error" , 1);
error_reporting(E_ALL);
// Get input object
$jinput = JFactory::getApplication()->input;
// Get posted data
$data = $jinput->get('jform', null, 'raw');
$file = $jinput->files->get('jform');
// renaming the file
$file_ext=strtolower(end(explode('.',JFile::makeSafe($file['pdf_file_path']['name']))));
$filename = round(microtime(true)) . '.' . $file_ext;
// Move the uploaded file into a permanent location.
if ( $filename != '' ) {
// Make sure that the full file path is safe.
$filepath = JPath::clean( JPATH_ROOT."/media/your_component_name/files/". $filename );
// Move the uploaded file.
if (JFile::upload( $file['pdf_file_path']['tmp_name'], $filepath )) {
echo "success :)";
} else {
echo "failed :(";
}
}
JRequest::setVar('jform', $data, 'post');
$return = parent::save($data);
return $return;
}
希望它对你有用:)