2016-08-11 2 views

ответ

0
      form xml :  
          <field name="con_image" type="file" label="" description="" hint="Image"/> 
      default.php 
          <div class="controls"> 
              <?php if (!empty($this->item->con_image) && file_exists(JPATH_SITE.'/images/contact_image/thumb_' . $this->item->con_image)) : ?> 
               <img src="<?php echo JRoute::_(JUri::root() . 'images/contact_image/thumb_' . $this->item->con_image, false);?>"> 
              <?php endif; ?> 
               <input type="hidden" name="jform[con_image]" id="jform_image_hidden" value="<?php echo $this->item->con_image; ?>" /> 
               <?php echo $this->form->renderField('con_image'); ?> 
             </div> 
      your view table file code for image upload: 

      $files = $app->input->files->get('jform', array(), 'raw'); 

        $array = $app->input->get('jform', array(), 'ARRAY'); 

        if (!empty($files['con_image']['name'])) 
        { 
         // Deleting existing files 
         $oldFiles = PlansHelpersPlans::getFiles($this->id, $this->_tbl, 'con_image'); 

         foreach ($oldFiles as $f) 
         { 
          $oldFile = JPATH_ROOT . '/images/contact_image/' . $f; 


          if (file_exists($oldFile)) 
          { 
           unlink($oldFile); 
          } 

         } 

         $this->con_image = ""; 
         $singleFile = $files['con_image']; 

          jimport('joomla.filesystem.file'); 

          // Check if the server found any error. 
          $fileError = $singleFile['error']; 
          $message = ''; 

          if ($fileError > 0 && $fileError != 4) 
          { 
           switch ($fileError) 
           { 
            case 1: 
             $message = JText::_('File size exceeds allowed by the server'); 
             break; 
            case 2: 
             $message = JText::_('File size exceeds allowed by the html form'); 
             break; 
            case 3: 
             $message = JText::_('Partial upload error'); 
             break; 
           } 

           if ($message != '') 
           { 
            $app->enqueueMessage($message, 'warning'); 

            return false; 
           } 
          } 
          elseif ($fileError == 4) 
          { 
           if (isset($array['con_image'])) 
           { 
            $this->con_image = $array['con_image']; 
           } 
          } 
          else 
          { 

           // Replace any special characters in the filename 
           jimport('joomla.filesystem.file'); 
           $filename = JFile::stripExt($singleFile['name']); 
           $extension = JFile::getExt($singleFile['name']); 
           $filename = preg_replace("/[^A-Za-z0-9]/i", "-", $filename); 
           $filename = rand()."-".$filename . '.' . $extension; 
           $uploadPath = JPATH_ROOT . '/images/contact_image/' . $filename; 

           $fileTemp = $singleFile['tmp_name']; 

           if (!JFile::exists($uploadPath)) 
           { 
            if (!JFile::upload($fileTemp, $uploadPath)) 
            { 
             $app->enqueueMessage('Error moving file', 'warning'); 

             return false; 
            } 
           } 

           //$this->con_image .= (!empty($this->con_image)) ? "," : ""; 
           $this->con_image = $filename; 
          } 

        }else{ 
         $this->con_image = $array['con_image']; 
        } 

Тем не менее у вас есть проблемы, то проверить здесь Скачать мой простой компонент: https://github.com/erakashpatel/Important-notes/blob/master/com_helloworld.zip ИЛИ https://github.com/erakashpatel/Important-notes/blob/master/com_test.zip

Я надеюсь, что его works.Thanks заранее.

0
for xml : 
      Joomla File form field type in xml: 
      https://docs.joomla.org/File_form_field_type 
      <field name="myfilevalue" type="file" label="Enter some text" description="Choose an image from your computer with maximum 100KB" size="10" accept="image/*" /> 


      OR 
      if your file field in html form then used : 
      <input type="file" name="jform[myfilevalue]" > 

Я надеюсь, что его помощь, еще обеспечить более описание пожалуйста

+0

Привет, спасибо за ваш ответ. Это то, что у меня уже есть. То, что я хочу сделать, это загрузить этот файл. Для этого мне нужно изменить атрибут на теге '

', чтобы быть 'enctype =" multipart/form-data "' – simon

+0

@simon: вы можете попробовать Javascript для установки enctype, как описано здесь, - http: // www .w3schools.com/jsref/tryit.asp? имя_файла = tryjsref_form_enctype2 – Irfan

Смежные вопросы