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

How to Save Uploaded File#39;s Name on Database(如何在数据库中保存上传文件的名称)
本文介绍了如何在数据库中保存上传文件的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

继续.- 将文件上传器添加到 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.

这篇关于如何在数据库中保存上传文件的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)