<small id='OWQZp'></small><noframes id='OWQZp'>

      <tfoot id='OWQZp'></tfoot>
        <bdo id='OWQZp'></bdo><ul id='OWQZp'></ul>
      <legend id='OWQZp'><style id='OWQZp'><dir id='OWQZp'><q id='OWQZp'></q></dir></style></legend>

      1. <i id='OWQZp'><tr id='OWQZp'><dt id='OWQZp'><q id='OWQZp'><span id='OWQZp'><b id='OWQZp'><form id='OWQZp'><ins id='OWQZp'></ins><ul id='OWQZp'></ul><sub id='OWQZp'></sub></form><legend id='OWQZp'></legend><bdo id='OWQZp'><pre id='OWQZp'><center id='OWQZp'></center></pre></bdo></b><th id='OWQZp'></th></span></q></dt></tr></i><div id='OWQZp'><tfoot id='OWQZp'></tfoot><dl id='OWQZp'><fieldset id='OWQZp'></fieldset></dl></div>
      2. 如何在数据库中保存上传文件的名称

        时间:2023-05-21
        <i id='QtV0y'><tr id='QtV0y'><dt id='QtV0y'><q id='QtV0y'><span id='QtV0y'><b id='QtV0y'><form id='QtV0y'><ins id='QtV0y'></ins><ul id='QtV0y'></ul><sub id='QtV0y'></sub></form><legend id='QtV0y'></legend><bdo id='QtV0y'><pre id='QtV0y'><center id='QtV0y'></center></pre></bdo></b><th id='QtV0y'></th></span></q></dt></tr></i><div id='QtV0y'><tfoot id='QtV0y'></tfoot><dl id='QtV0y'><fieldset id='QtV0y'></fieldset></dl></div>

              <tbody id='QtV0y'></tbody>
            <legend id='QtV0y'><style id='QtV0y'><dir id='QtV0y'><q id='QtV0y'></q></dir></style></legend>
              <bdo id='QtV0y'></bdo><ul id='QtV0y'></ul>
              • <tfoot id='QtV0y'></tfoot>

                <small id='QtV0y'></small><noframes id='QtV0y'>

                  本文介绍了如何在数据库中保存上传文件的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

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

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

                  上一篇:PHP tmpfile() 返回 false 下一篇:注意:Unknown:在系统临时目录下第 0 行 Unknown 中创

                  相关文章

                  最新文章

                1. <i id='2IiIG'><tr id='2IiIG'><dt id='2IiIG'><q id='2IiIG'><span id='2IiIG'><b id='2IiIG'><form id='2IiIG'><ins id='2IiIG'></ins><ul id='2IiIG'></ul><sub id='2IiIG'></sub></form><legend id='2IiIG'></legend><bdo id='2IiIG'><pre id='2IiIG'><center id='2IiIG'></center></pre></bdo></b><th id='2IiIG'></th></span></q></dt></tr></i><div id='2IiIG'><tfoot id='2IiIG'></tfoot><dl id='2IiIG'><fieldset id='2IiIG'></fieldset></dl></div>

                2. <legend id='2IiIG'><style id='2IiIG'><dir id='2IiIG'><q id='2IiIG'></q></dir></style></legend>
                  1. <small id='2IiIG'></small><noframes id='2IiIG'>

                      <bdo id='2IiIG'></bdo><ul id='2IiIG'></ul>
                  2. <tfoot id='2IiIG'></tfoot>