如何在数据库中保存上传文件的名称

4

续 - 在Joomla管理组件中添加文件上传器

我已经可以上传文件并将其保存到磁盘上,但是它没有将文件名保存到数据库中。

我该怎么做?

这里是控制器代码 -

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();
    }
}

XML中的表单字段 -

<field name="invoice" type="file"/>

更新: 在@Andras Gera的代码中加入以下行后,它可以正常工作。

$data = JRequest::getVar( 'jform', null, 'post', 'array' );
$data['invoice'] = strtolower( $file['name']['invoice'] );

JRequest::setVar('jform', $data );
5个回答

7
我遇到了同样的问题,也许我们可以一起解决。这是我的代码:
/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();
    }

}

如果在将$data发送给parent::save($data)之前打印出它,它会包含您要保存的正确字段,但实际上并没有保存。我尝试使用input type=text而不是type=file,它可以正确保存。
我还尝试了另一种方法,如:input type=file和name=pdf_file,然后我添加了一个隐藏字段name=file default=""。然后我将此隐藏字段值设置为文件名,但没有成功。也许我做错了什么。继续努力找到解决方案。

我认为我们应该将更改后的 $data 传回 POST 方法,因为 parent::save() 返回系统 save()。不知何故,如果我将 $data 添加到 save() 函数中,它就无法正常工作。保存所有内容,但是数据库中只有文件字段为空。 - Andras Gera
你找到任何方法来做它了吗? - ChamingaD
是的,问题已解决!感谢Irfan提到了setVar函数。他的解决方案不起作用,因为它会创建另一条记录,而不是将所需的变量放入$_REQUEST数组中的jform数组中。我的解决方案是覆盖jform变量。 - Andras Gera
我将完善上面的代码以使其更好。在此阶段,当您编辑记录时,将会丢失文件变量。 - Andras Gera
1
已经修复了。请仔细检查XML和PHP代码。我已经添加了一个隐藏字段,以便在您编辑表单的其他部分时保留原始文件名。当您选择一个新文件时,它将被替换。还有更多需要修复的地方,但我希望这能够帮助您继续前进。干杯! - Andras Gera
非常感谢,现在已经可以工作了。必须添加以下几行代码: $data = JRequest::getVar( 'jform', null, 'post', 'array' ); $data['invoice'] = strtolower( $file['name']['invoice'] );JRequest::setVar('jform', $data ); - ChamingaD

1
    //import joomlas filesystem functions, we will do all the filewriting with joomlas functions
        jimport('joomla.filesystem.file');
        jimport('joomla.filesystem.folder');

      //this is the name of the field in the html form, filedata is the default name for swfupload
    $fieldName = 'Filedata';

        //the name of the file in PHP's temp directory that we are going to move to our folder
        $fileTemp = $_FILES[$fieldName]['tmp_name'];


        //always use constants when making file paths, to avoid the possibilty of remote file inclusion
        $uploadPath = JPATH_SITE.DS.'path'.DS.'path'.DS.$fileName;

        if(!JFile::upload($fileTemp, $uploadPath)) 
        {
                echo JText::_( 'ERROR MOVING FILE' );
                return;
        }
        else
        {
         //Updating the db with the $fileName.
         $db =& JFactory::getDBO();   
         $query = $db->getQuery(true);
         $query->update($db->nameQuote(TABLE_PREFIX.'table_name'));
         $query->set($column.' = '.$db->quote($fileName));
         $query->where($db->nameQuote('id').'='.$db->quote($id));             
         $db->setQuery($query);
         $db->query(); 
         }

$column - 文件的数据库列名 $fileName - 文件名

如果文件成功上传,则运行查询。


我按照你说的做了,但是还是不起作用 :/ 这是我组件中保存文件的部分 - http://pastebin.com/yKiiBigu .. 将$column替换为我的表中的VARCHAR类型的$invoice。$db的var_dump - http://pastebin.com/MyCdKrAB - ChamingaD
你漏掉了JFile::upload($fileTemp, $uploadPath)的if语句。请正确参考示例并修改你的代码。如果遇到任何问题,请提出。 - Techie
嗯,我认为问题出在查询上。$column(在我的情况下是$invoice)未定义。还有$id。 - ChamingaD
这是修改后的代码 - http://pastebin.com/ytRLEJJZ $db 的 var_dump - http://pastebin.com/JqJnQMZk - ChamingaD
#__自动将数据库前缀放在哪里?$query->update($db->nameQuote('#__invoicemanager')); - ChamingaD
显示剩余4条评论

1

将文件名设置为请求变量,因为它现在是一个 $_FILES 变量

JRequest::setVar('jform[invoice]',$file['name']['invoice'] );

//完整代码

   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 );

                JRequest::setVar('jform[invoice]',$file['name']['invoice'] );
            }



            return parent::save();
        }

}

@ChamingaD:测试一下-> JRequest::setVar('column','test' );如果这个可以工作,那么就是一些命名问题。 - Irfan
@ChamingaD:列名是什么? - Irfan
让我们在聊天中继续这个讨论:http://chat.stackoverflow.com/rooms/18164/discussion-between-irfan-and-chamingad - Irfan

1

您可以使用php的move_uploaded_file()函数


文件已成功保存在磁盘上。上传后,我只需要将它的名称存储在数据库中。 - ChamingaD

0
在 Joomla 3.2.x 上,我必须重写我的模型类的保存函数,以将上传的文件名保存到数据库中,就像这样。
public function save($data){
  $input = JFactory::getApplication()->input;       
  $files = $input->files->get('jform');
  $fieldName = 'thumbnail';
  $data['thumbnail'] = $files[$fieldName]['name'];              
  return parent::save($data);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接