Exemple #1
0
 /**
  * 
  * Uploads a file if the upload is used
  * copy the file to the tmp directory if a local file is used
  * unpack a packed file is used
  */
 function upload()
 {
     $mainframe = JFactory::getApplication();
     jimport('joomla.filesystem.file');
     //Retrieve file details from uploaded file, sent from upload form:
     $uploaded_file = JRequest::getVar('uploaded_file', null, 'files', 'array');
     //do we use an existing file on server or an uploades file?
     if (empty($uploaded_file['name'])) {
         $existing_file = JRequest::getVar('existing_file', null, 'post', 'string');
         $existing_file_name = JFile::makeSafe(Jfile::getname(Jfile::stripExt($existing_file)));
         $existing_file_extension = JFile::makeSafe(JFile::getExt($existing_file));
         $existing_file_folder_name = JFolder::makeSafe(strstr($existing_file, $existing_file_name, true));
         // $mainframe->getCfg('tmp_path') . DS . JFile::makeSafe(JFile::getName($existing_file));
         $tempfile = tempnam($mainframe->getCfg('tmp_path'), $existing_file_name);
         $fDestName = $tempfile . "." . $existing_file_extension;
         //we don't need the file itseld, we just need the name
         unlink($tempfile);
         JFile::copy(JPATH_ROOT . $existing_file_folder_name . $existing_file_name . "." . $existing_file_extension, $fDestName);
     } else {
         $fDestName = $mainframe->getCfg('tmp_path') . DS . JFile::makeSafe($uploaded_file['name']);
         $fNameTmp = $uploaded_file['tmp_name'];
         if (!JFile::upload($fNameTmp, $fDestName)) {
             //Display the back button:
             JToolBarHelper::back();
             $this->setError('The file could not be uploaded.');
         }
     }
     if (count($this->getErrors() == 0)) {
         $returnURL = JURI::base() . 'index.php?option=com_k2import&task=selectcategory&file=' . JFile::getName($fDestName);
         if (strtolower(JFile::getExt($fDestName)) != 'csv') {
             //we will try to use the file as archive
             jimport('joomla.filesystem.archive');
             $import_tmp_dir = $mainframe->getCfg('tmp_path') . DS . 'k2_import';
             if (JArchive::extract($fDestName, $import_tmp_dir)) {
                 $csv_files = JFolder::files($import_tmp_dir, '.csv');
                 foreach ($csv_files as $csv_file) {
                     if (!JFile::move($csv_file, JFile::makeSafe($csv_file), $import_tmp_dir)) {
                         $this->setError('The file ' . $csv_file . ' could not be renamed.');
                         return false;
                     }
                 }
                 $this->setRedirect($returnURL . '&modus=archive', 'The file was successful uploaded and extracted');
             } else {
                 JToolBarHelper::back();
                 $this->setError('The file could not be extracted.');
             }
         } else {
             $this->setRedirect($returnURL, 'The file was successful uploaded');
         }
     }
 }