We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 44195
    • 293 Posts
    I've managed to put together a processor that will upload a file to a designated location and also create a database record. Only problem is the filename is not being saved to the database. Instead just the word "Array" is being saved there.
    What's the recommended way to get the filename string?


    Here's the Modext/Extjs bit on a form:
    ,items      : [{
        id: this.ident+'-filename'
        ,inputType: 'file'
        ,fieldLabel: _('mycomponent.browse_file_label')
        ,name: 'filename'
        ,height: 30
    },
    


    I assume the file is represented as an object/array and I need to somehow just get to the filename but not sure how.

    Processor code:
    class ArtworkCreateProcessor extends modObjectCreateProcessor {
        public $classKey = 'artworks';
        public $languageTopics = array('mycomponent:default');
        public $objectType = 'mycomponent';
        
        /** @var artworks $object */
        public $object;
    
        /** @var modMediaSource $source */
        public $source;
    
        public function checkPermissions() {
            return $this->modx->hasPermission('file_upload');
        }
    
        public function getLanguageTopics() {
            $langs = parent::getLanguageTopics();
            $langs[] = 'mycomponent';
            return $langs;
        }
    
        public function initialize() {
            $this->setDefaultProperties(array(
                'source' => 1,
                'gallery' => false,
            ));
            if (!$this->getProperty('gallery')) return $this->modx->lexicon('file_folder_err_ns');
            return parent::initialize();
        }
    
        public function process() {
            if (!$this->getSource()) {
                return $this->failure($this->modx->lexicon('permission_denied'));
            }
            $this->source->setRequestProperties($this->getProperties());
            $this->source->initialize();
            if (!$this->source->checkPolicy('create')) {
                return $this->failure($this->modx->lexicon('permission_denied'));
            }
            $success = $this->source->uploadObjectsToContainer($this->getProperty('gallery'),$_FILES);
    
            if (empty($success)) {
                $msg = '';
                $errors = $this->source->getErrors();
                foreach ($errors as $k => $msg) {
                    $this->modx->error->addField($k,$msg);
                }
                return $this->failure($msg);
            }
            return parent::process();
        }
    
        /**
         * Get the active Source
         * @return modMediaSource|boolean
         */
        public function getSource() {
            $this->modx->loadClass('sources.modMediaSource');
            $this->source = modMediaSource::getDefaultSource($this->modx,$this->getProperty('source'));
            if (empty($this->source) || !$this->source->getWorkingContext()) {
                return false;
            }
            return $this->source;
        }
    
    }
    return 'ArtworkCreateProcessor';
    

    This question has been answered by Bruno17. See the first response.

    [ed. note: muzzstick last edited this post 12 years, 7 months ago.]
      I'm lead developer at Digital Penguin Creative Studio in Hong Kong. https://www.digitalpenguin.hk
      Check out the MODX tutorial series on my blog at https://www.hkwebdeveloper.com
      • 4172
      • 5,888 Posts
      you forgot to post your code of your processor
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 44195
        • 293 Posts
        Oops! I've edited my post with the processor code
          I'm lead developer at Digital Penguin Creative Studio in Hong Kong. https://www.digitalpenguin.hk
          Check out the MODX tutorial series on my blog at https://www.hkwebdeveloper.com
        • discuss.answer
          • 4172
          • 5,888 Posts
          you should get the uploaded filename(s) by looping thrue the $_FILES - array

          $filenames = array();
          if (is_array($_FILES)) {
              foreach ($_FILES as $file) {
                  if (!empty($file['name'])) {
                      $filenames[] = $file['name'];
                  }
              }
          }
          


          then you will need to put the filename to your db-field before saving.
            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 44195
            • 293 Posts
            Ah! The $_FILES array. Can't believe I didn't think of that.
            Thanks very much Bruno!

            My updated process() function in case it helps anyone:

            public function process() {
                if (!$this->getSource()) {
                    return $this->failure($this->modx->lexicon('permission_denied'));
                }
                $this->source->setRequestProperties($this->getProperties());
                $this->source->initialize();
                if (!$this->source->checkPolicy('create')) {
                    return $this->failure($this->modx->lexicon('permission_denied'));
                }
                $success = $this->source->uploadObjectsToContainer($this->getProperty('gallery'),$_FILES);
            
                if (empty($success)) {
                    $msg = '';
                    $errors = $this->source->getErrors();
                    foreach ($errors as $k => $msg) {
                        $this->modx->error->addField($k,$msg);
                    }
                    return $this->failure($msg);
                }
            
                // Cheers Bruno!
                $filenames = array();
                if (is_array($_FILES)) {
                    foreach ($_FILES as $file) {
                        if (!empty($file['name'])) {
                            $filenames[] = $file['name'];
                        }
                    }
                }
                $this->setProperty('filename', $filenames[0]);
            
                return parent::process();
            }
            

              I'm lead developer at Digital Penguin Creative Studio in Hong Kong. https://www.digitalpenguin.hk
              Check out the MODX tutorial series on my blog at https://www.hkwebdeveloper.com