[英]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;
}
希望它對你有用:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.