Php 如何保存上传的文件';数据库中的用户名

Php 如何保存上传的文件';数据库中的用户名,php,file-upload,joomla,joomla2.5,Php,File Upload,Joomla,Joomla2.5,续 我可以上传文件并保存在磁盘上。但它不会将文件名保存在数据库中 我怎么做 这是控制器- class InvoiceManagerControllerInvoiceManager extends JControllerForm { function save(){ $file = JRequest::getVar('jform', null, 'files', 'array'); $path = JPATH_BASE; // Make t

我可以上传文件并保存在磁盘上。但它不会将文件名保存在数据库中

我怎么做

这是控制器-

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"/>

您可以使用php
move\u uploaded\u file()
函数

    //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-文件的db列名 $fileName-文件名


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

我遇到了相同的问题,也许我们可以一起继续。这是我的密码:

/管理员/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>

JP出版
六月出版
及 /管理员/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();
    }

}

在请求变量中设置文件名,因为它现在是$\u 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();
        }

}

在Joomla3.2.x上,我必须重写模型类的save函数,将上传的文件名保存到db,如下所示

public function save($data){
  $input = JFactory::getApplication()->input;       
  $files = $input->files->get('jform');
  $fieldName = 'thumbnail';
  $data['thumbnail'] = $files[$fieldName]['name'];              
  return parent::save($data);
}

文件已成功保存在磁盘上。我只需要在上传后将其名称存储在数据库中。谢谢Dasun,现在上传文件就可以了。我一直在更新数据库:/I我按照你提到的那样做了。但它不起作用:/这是我的组件中的文件保存部分-。。将$invoice列替换为$invoice,该VARCHAR位于我的表中。var_dump为$db-您缺少JFile::upload($fileTemp,$uploadPath)的if语句。请正确参考eg并更改代码。若你们遇到任何问题,请提问。嗯,我认为问题来自于询问$未定义列($invoice,在我的情况下)。这里是修改后的代码-var_dump of$db-我认为我们应该将修改后的$data传回POST方法,因为parent::save()返回到系统save()。不知何故,如果我将$data添加到save()函数中,它就不会得到很好的效果。保存所有内容,但数据库中只有文件字段为空。您是否找到了执行此操作的方法?是的,已解决!感谢Irfan提到setVar函数。他的解决方案不起作用,因为它创建了另一条记录,而不是将所需的变量放入$\u请求数组中的jform数组中。我的解决方案是重写jform变量。我将完成上面的代码以使其更好。在此阶段,编辑记录时会丢失文件变量。这是固定的。仔细检查xml和php代码。我添加了一个隐藏字段,以便在编辑表单的其他部分时保留原始文件名。选择新文件后,将替换它。还有更多的问题需要解决,但我希望这会对未来有很大帮助。Cheers@ChamingaD:testthis->JRequest::setVar('column','Test');如果这是可行的,还有一些命名problem@ChamingaD当前位置列名称是什么?让我们来看看
public function save($data){
  $input = JFactory::getApplication()->input;       
  $files = $input->files->get('jform');
  $fieldName = 'thumbnail';
  $data['thumbnail'] = $files[$fieldName]['name'];              
  return parent::save($data);
}