2012-12-12 48 views
2

我在創建自定義字段的上傳機制時遇到了困難:libraries/joomla/filesystem/file.php。該表單在視圖中設置了正確的enctype,它只是不上傳。下面是我的組件/模型/場/ uploadfield.php代碼:Joomla自定義組件上傳表單

protected function getInput() 
{ 

    //Retrieve file details from uploaded file, sent from upload form 
    $file = JFactory::getApplication()->input->get($this->name, null, 'files', 'array'); 

    //Import filesystem libraries. Perhaps not necessary, but does not hurt 
    jimport('joomla.filesystem.file'); 

    //Clean up filename to get rid of strange characters like spaces etc 
    $filename = JFile::makeSafe($file['name']); 

    //Set up the source and destination of the file 
    $src = $file['tmp_name']; 
    $dest = JPATH_COMPONENT . DIRECTORY_SEPARATOR . $filename; 

    //First check if the file has the right extension, we need jpg only 
    if (strtolower(JFile::getExt($filename)) == 'jpg') { 
     if (JFile::upload($src, $dest)) { 
     //Redirect to a page of your choice 
     } else { 
     //Redirect and throw an error message 
     } 
    } else { 
    //Redirect and notify user file is not right extension 
    } 

    return '<input type="file" name="' . $this->name . '" id="' . $this->id . '"' . ' />'; 
} 

上午我甚至會對此具有處於getInput()函數上傳機制的正確方法?它應該在模型中嗎?我真的堅持如何使這項工作,一直試圖遵循:http://docs.joomla.org/How_to_use_the_filesystem_package但它忽略了上傳代碼應該去哪裏?

非常感謝

回答

1

好了 - 所以我解決了這個問題:

我使用的getInput()函數的自定義字段文件只是做表格上顯示的輸入域的工作,並在控制器中創建一個函數來完成節約。去有點像這樣:

function save(){ 
    jimport('joomla.filesystem.file'); 

    $jFileInput = new JInput($_FILES); 
    $theFiles = $jFileInput->get('jform',array(),'array'); 

    $filepath = "DESTINATIONFILEPATH" 
    JFile::upload($theFiles['tmp_name']['filename'], $filepath); 

    return parent::save(); 
} 

只需要制定的文件名保存到JInput「jform」陣列,這樣的文件名獲取的更新到數據庫的Joomla 3.0的方式。這只是覆蓋現有jform數據:

$jinput = JFactory::getApplication()->input; 
$jinput->set('jform',$arraytoadd); 

我在這裏問的問題,如果有人有興趣: Adding field input to JForm using JInput

3

嘗試使用這就是我對我的部件之一使用了以下:

function getInput(){ 

    jimport('joomla.filesystem.file'); 

    $jinput = JFactory::getApplication()->input; 
    $fileInput = new JInput($_FILES); 
    $file = $fileInput->get('image', null, 'array'); 

    if(isset($file) && !empty($file['name'])) { 
     $filename = JFile::makeSafe($file['name']); 
     $src = $file['tmp_name']; 
     $data['image']=$filename; 

     $dest = JPATH_COMPONENT . '/' . $filename; 

     if (strtolower(JFile::getExt($filename)) == 'jpg') { 
      if(!JFile::upload($src, $dest)) { 
      return false; 
      } 
     } 
     else { 
      JError::raiseWarning('', 'File type not allowed!.'); 
      return false; 
     } 
    } 
    return true; 
} 

請注意,下面的代碼:

$file = JRequest::getVar('image', null, 'files', 'array');

圖片「來自輸入字段的名稱,如下所示:

<input type="file" id="" name="image" /> 

因此,改爲任何名稱給你的輸入字段。

+0

你好,非常感謝您的答覆 - 我已經試過這一點,它不似乎工作 - 該字段出現在視圖上,我可以選擇一個文件,但似乎並沒有在任何地方上傳?我使用Joomla 3.0,所以JRequest :: getVar現在已經被替換爲:JFactory :: getApplication() - > input-> get()。 – mousebat

+0

啊,我不知道你在使用Joomla 3.0。我會嘗試進行必要的更改 – Lodder

+0

@ user1717113 - 我已更新我的答案 – Lodder