问题描述
续 -
我可以上传文件并将其保存在磁盘上。但是它没有在数据库上保存文件名。
我该怎么做?
这里是控制器 -
$ class InvoiceManagerControllerInvoiceManager extends JControllerForm
{
function save(){
$ 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);
}
return parent :: save();
$ <$ p $
$ b
< field name =invoicetype =file/>
更新:
在@Andras Gera代码中添加以下行后工作
$ $ $ $ $ $ $ $ $ data $ JRequest :: getVar('jform',null,'post','array');
$ data ['invoice'] = strtolower($ file ['name'] ['invoice']);
JRequest :: setVar('jform',$ data);
我遇到了同样的问题,也许我们可以一起前进。这里是我的代码:
$ / b
$ b $ / p $ / $ / $ / $ p> <?xml version =1.0encoding =utf-8?>
< form addrulepath =/ administrator / components / com_gonewsletter / models / rules>
< fieldset name =details>
< field
name =id
type =hidden
/>
name =title
type =text
label =COM_GONEWSLETTER_EDIT_TITLE_LABEL
description =COM_GONEWSLETTER_EDIT_TITLE_DESC
size = 40
class =inputbox
required =true
default =
/>
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
/> ;
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>
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>
和
/administrator/components/com_comp_name/controllers/edit.php
<?php
//不能直接访问这个文件
defined('_ JEXEC')or die('禁止进入');
//导入Joomla控制器库
jimport('joomla.application.component.controllerform');
$ b / **
* GoNewsletter控制器
* /
类GoNewsletterControllerEdit扩展JControllerForm
{
函数__construct($ config = array() ){
$ this-> view_list ='List';
parent :: __ construct($ config);
}
function save(){
// -------------------------- - 上传文件---------------------
// Neccesary库和变量
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']);
//将上传的文件移到永久位置。
if($ filename!=''){
//确保完整的文件路径是安全的。
$ 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);
返回parent :: save();
$ p $如果你打印出$ data在将其发送到parent :: save($ data)之前,它包含要保存的正确字段,但不包含。我尝试使用输入类型=文本,而不是类型=文件,并保存正确。
我尝试另一种方式,如:输入类型=文件和名称= 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.
这篇关于如何保存数据库上传的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!