2012-10-17 100 views
4

繼續。 - Add File Uploader to Joomla Admin Component如何保存數據庫上傳的文件名

我能夠上傳文件並將其保存在磁盤上。但它沒有在數據庫上保存文件名。

我該怎麼辦?

這裏是控制器 -

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

UPDATE: 增加從@Andras格拉代碼採取以下幾行後工作

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

JRequest::setVar('jform', $data); 

回答

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> 

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

} 

如果您在將數據發送給parent :: save($ data)之前打印出$ data,則它包含要保存的正確字段,但不包含。我試圖使用輸入type = text而不是type = file,並且它可以正確保存。

我嘗試了另一種方式,例如:input type = file和name = pdf_file,之後我添加了一個隱藏字段name = file default =「」。然後我將這個隱藏的字段值設置爲文件名,但沒有成功。也許我做錯了什麼。繼續找出一些事情。請求變量

+0

我認爲我們應該將更改後的$數據傳遞迴POST方法,因爲parent :: save()會返回到系統save()。不知何故,如果我添加$數據save()函數它不會很好。保存所有內容,但只有文件字段在數據庫中爲空。 –

+0

你找到了嗎? – ChamingaD

+0

是的,它解決了!感謝Irfan提及setVar函數。他的解決方案不起作用,因爲它創建了另一條記錄,而不是將所需的變量放入$ _REQUEST數組中的jform數組中。 我的解決方案是覆蓋jform變量。 –

1

你可以使用php move_uploaded_file()功能

+0

文件成功保存在磁盤上。上傳後我只需要在數據庫中存儲它的名字。 – 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(); 
     } 

$列 - 文件 $文件名分貝列名 - 如果文件被成功上傳的文件名

查詢跑去。

+0

感謝Dasun,現在文件上傳好了。我被困在更新數據庫:/ – ChamingaD

+0

檢查修訂 – Techie

+0

我照你所說的。但它不工作:/這裏是我的組件中的文件保存部分 - http://pastebin.com/yKiiBigu ..用$ invoice替換了$ column,其中VARCHAR在我的表中。 $ db的var_dump - http:// pastebin。com/MyCdKrAB – ChamingaD

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

} 
+0

嗯不起作用。列的文件名仍然是空的:/ – ChamingaD

+0

@ChamingaD:您的代碼是否正在上傳文件? – Irfan

+0

是的,它成功上傳文件:) – ChamingaD

0

上的Joomla 3.2.x中,我要重寫保存我的模型類的功能將上傳的文件名保存到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); 
} 
相關問題