問題描述
繼續.- 將文件上傳器添加到 Joomla 管理組件>
我可以上傳文件并將其保存在磁盤上.但它沒有在數據庫中保存文件名.
我該怎么做?
這是控制器 -
class InvoiceManagerControllerInvoiceManager 擴展 JControllerForm{函數保存(){$file = JRequest::getVar('jform', null, 'files', 'array');$path = JPATH_BASE;//確保文件名安全.jimport('joomla.filesystem.file');$file['name']['invoice'] = JFile::makeSafe($file['name']['invoice']);//將上傳的文件移動到一個永久位置.if (isset($file['name']['invoice'])) {//確保完整的文件路徑是安全的.$filepath = JPath::clean($path.DS."components".DS."com_invoicemanager".DS."files".DS.strtolower($file['name']['invoice']));//移動上傳的文件.JFile::upload( $file['tmp_name']['invoice'], $filepath );}返回父級::保存();}}
XML 中的表單字段 -
更新:添加以下來自@Andras Gera 代碼的行后工作
$data = JRequest::getVar('jform', null, 'post', 'array');$data['invoice'] = strtolower( $file['name']['invoice'] );JRequest::setVar('jform', $data );
我也遇到了同樣的問題,也許我們可以一起前進.這是我的代碼:
/administrator/components/com_comp_name/models/forms/edit.xml
<選項value="1">JPUBLISHED</option><選項value="0">JUNPUBLISHED</option></field><字段類型=文件"名稱="pdf_file"標簽="COM_GONEWSLETTER_EDIT_FILE_LABEL"默認="描述="COM_GONEWSLETTER_EDIT_FILE_DESC"大小=40"接受=申請/pdf"類=文件上傳器"/><字段名稱=文件"類型=隱藏"/></fieldset></表單>
和/administrator/components/com_comp_name/controllers/edit.php
view_list = '列表';parent::__construct($config);}函數保存(){//--------------------- 上傳文件 ---------------------//必要的庫和變量jimport('joomla.filesystem.folder');jimport('joomla.filesystem.file');$data = JRequest::getVar('jform', null, 'post', 'array');//如果圖像文件夾中不存在 gonewsleter 文件夾,則創建該文件夾if ( !JFolder::exists( JPATH_SITE . DS . "images" . DS . "gonewsletter" ) ) {JFolder::create( JPATH_SITE . DS . "images" . DS . "gonewsletter" );}//從請求中獲取文件數據數組.$file = JRequest::getVar('jform', null, 'files', 'array');//確保文件名安全.$filename = JFile::makeSafe($file['name']['pdf_file']);//將上傳的文件移動到一個永久位置.如果($文件名!= ''){//確保完整的文件路徑是安全的.$filepath = JPath::clean( JPATH_SITE . DS . 'images' . DS . 'gonewsletter' . DS . strtolower( $filename ) );//移動上傳的文件.JFile::upload( $file['tmp_name']['pdf_file'], $filepath );//在保存到數據庫之前更改 $data['file'] 值$data['file'] = strtolower( $filename );}//--------------------- 文件上傳結束 ------------------------JRequest::setVar('jform', $data );返回父級::保存();}}
如果您在將 $data 發送到 parent::save($data) 之前打印它,它包含您想要保存的正確字段,但它沒有.我嘗試使用輸入 type=text 而不是 type=file 并正確保存.
我嘗試了另一種方式,例如:輸入類型=文件和名稱=pdf_file,然后我添加了一個隱藏字段名稱=文件默認=".然后我將此隱藏字段值設置為文件名但沒有成功.也許我做錯了什么.繼續想辦法.
Cont. - Add File Uploader to Joomla Admin Component
I could able to upload file and save it on disk. But its not saving file name on the database.
How can i do it ?
Here is the controller -
class InvoiceManagerControllerInvoiceManager extends JControllerForm
{
function save(){
$file = JRequest::getVar('jform', null, 'files', 'array');
$path = JPATH_BASE;
// Make the file name safe.
jimport('joomla.filesystem.file');
$file['name']['invoice'] = JFile::makeSafe($file['name']['invoice']);
// Move the uploaded file into a permanent location.
if (isset($file['name']['invoice'])) {
// Make sure that the full file path is safe.
$filepath = JPath::clean($path. DS ."components". DS ."com_invoicemanager". DS ."files". DS .strtolower($file['name']['invoice']));
// Move the uploaded file.
JFile::upload( $file['tmp_name']['invoice'], $filepath );
}
return parent::save();
}
}
Form field in XML -
<field name="invoice" type="file"/>
UPDATE: worked after adding following lines taken from @Andras Gera code
$data = JRequest::getVar( 'jform', null, 'post', 'array' );
$data['invoice'] = strtolower( $file['name']['invoice'] );
JRequest::setVar('jform', $data );
I've ran into the same problem, maybe we can go forward together. Here is my codes:
/administrator/components/com_comp_name/models/forms/edit.xml
<?xml version="1.0" encoding="utf-8"?>
<form addrulepath="/administrator/components/com_gonewsletter/models/rules">
<fieldset name="details">
<field
name="id"
type="hidden"
/>
<field
name="title"
type="text"
label="COM_GONEWSLETTER_EDIT_TITLE_LABEL"
description="COM_GONEWSLETTER_EDIT_TITLE_DESC"
size="40"
class="inputbox"
required="true"
default=""
/>
<field
name="date"
type="calendar"
label="COM_GONEWSLETTER_EDIT_DATE_LABEL"
description="COM_GONEWSLETTER_EDIT_DATE_DESC"
size="40"
class="inputbox"
required="true"
default=""
format="%Y-%m-%d"
/>
<field
name="published"
type="list"
label="JSTATUS"
description="COM_GONEWSLETTER_EDIT_PUBLISHED_DESC"
class="inputbox"
size="1"
default="0">
<option
value="1">JPUBLISHED</option>
<option
value="0">JUNPUBLISHED</option>
</field>
<field
type="file"
name="pdf_file"
label="COM_GONEWSLETTER_EDIT_FILE_LABEL"
default=""
description="COM_GONEWSLETTER_EDIT_FILE_DESC"
size="40"
accept="application/pdf"
class="fileuploader"
/>
<field
name="file"
type="hidden"
/>
</fieldset>
</form>
and /administrator/components/com_comp_name/controllers/edit.php
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import Joomla controllerform library
jimport('joomla.application.component.controllerform');
/**
* GoNewsletter Controller
*/
class GoNewsletterControllerEdit extends JControllerForm
{
function __construct($config = array()) {
$this->view_list = 'List';
parent::__construct($config);
}
function save(){
// ---------------------------- Uploading the file ---------------------
// Neccesary libraries and variables
jimport( 'joomla.filesystem.folder' );
jimport('joomla.filesystem.file');
$data = JRequest::getVar( 'jform', null, 'post', 'array' );
// Create the gonewsleter folder if not exists in images folder
if ( !JFolder::exists( JPATH_SITE . DS . "images" . DS . "gonewsletter" ) ) {
JFolder::create( JPATH_SITE . DS . "images" . DS . "gonewsletter" );
}
// Get the file data array from the request.
$file = JRequest::getVar( 'jform', null, 'files', 'array' );
// Make the file name safe.
$filename = JFile::makeSafe($file['name']['pdf_file']);
// Move the uploaded file into a permanent location.
if ( $filename != '' ) {
// Make sure that the full file path is safe.
$filepath = JPath::clean( JPATH_SITE . DS . 'images' . DS . 'gonewsletter' . DS . strtolower( $filename ) );
// Move the uploaded file.
JFile::upload( $file['tmp_name']['pdf_file'], $filepath );
// Change $data['file'] value before save into the database
$data['file'] = strtolower( $filename );
}
// ---------------------------- File Upload Ends ------------------------
JRequest::setVar('jform', $data );
return parent::save();
}
}
If you print out the $data before send it to parent::save($data) it contains the right fields you want to save, but it doesn't. I tried to use an input type=text instead of type=file and it saves correctly.
I tried another way like: input type=file and name=pdf_file, after then I added a hidden field name=file default="". And then I've set up this hidden field value to filename without success. Maybe I was doing something wrong. Keep continue to figure out something.
這篇關于如何在數據庫中保存上傳文件的名稱的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!