如何在数据库中保存上传文件的名称
继续.- 将文件上传器添加到 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.
相关文章