Jump to content

destinationPath and required field warnings in custom module upload


Recommended Posts

Hey all, I've looked at some other threads and github issues, but didn't find what I was looking for:
https://processwire.com/talk/topic/21616-file-upload-inside-custom-module/
https://processwire.com/talk/topic/619-what-does-it-mean-destinationpath-is-empty-or-does-not-exist-upload_file/
https://github.com/processwire/processwire-issues/issues/885

I'm on PW version 3.0.165.

I have a module using InputfieldFile to upload a CSV, then move it to a unique filename within a subdirectory in the module. Then it processes the CSV into a db table. That's actually all working smoothly, except I still get error messages on upload "destinationPath is empty or does not exist [field label]" and "Missing required value [field name]"

Below is the relevant code. A couple notes:

1. I have both `required` and `requiredAttr` set because I wanted to avoid them clicking submit without selecting a file (it's happened, haha). I've tried turning those off and it fixes the second error "Missing required value" but not the first one about destinationPath

2. I can't seem to debug the $destination created by tempDir(), because I think it is removed when the request ends, but I presume it must be working OK because the CSV file ends up in the `/.uploads/` directory I specify, and is processed into a database table. 

3. `/site/assets/cache/` directory is chmod 0777 and owned by "nobody" (yes, I know, I need to tighten this down :)

Thanks for any assistance! Worst case I guess I can tell them to ignore the red message since it is actually working.

<?php
$destination = $this->files->tempDir($this)->get();
// debugging shows $destination = /site/assets/cache/WireTempDir/.ProcessMemberScorecard/0/
$form = $this->modules->get('InputfieldForm');
$form->attr('action', $this->page->url . 'upload-scores');
$form->attr('enctype', 'multipart/form-data');

$wrapper = new InputfieldWrapper();
$wrapper->importArray(
    [
        [
            'type' => 'file',
            'name' => 'scores_upload',
            'label' => 'Scores File',
            'description' => 'Select a CSV file to upload',
            'required' => true,
            'requiredAttr' => true,
            'icon' => 'upload',
            'extensions' => 'csv',
            'destinationPath' => $destination,
            'maxFiles' => 1,
            'maxFilesize' => 10 * 1024 * 1024,
            'noAjax' => true,
            'descrptionRows' => 0,
        ],
        // additional checkbox fields, not relevant
    ]
);
$form->append($wrapper);

if ($this->input->requestMethod('POST')) {
    $form->processInput($this->input->post);

    $ul = new WireUpload('scores_upload');
    $ul->setValidExtensions(['csv']);
    $ul->setMaxFiles(1);
    $ul->setOverwrite(true);
    $ul->setDestinationPath($this->config->paths($this) . '.uploads/');

    $dt = new DateTime();
    $tmpname = bin2hex(random_bytes(8));
    $ul->setTargetFilename(
        sprintf('%s-%s.csv',
            $dt->format('Y-m-d'),
            $tmpname
        )
    );

    $results = $ul->execute();
    if ($errors = $ul->getErrors()) {
        foreach ($errors as $error) {
            $this->error($error);
        }
        return $form->render();
    }

    if (!$results) {
        // older, probably not needed since both required and requiredAttr are set
        $this->error('Please select a CSV file to upload');
        return $form->render();
    }

    // handle processing at this point, 
    // then redirect to the next step where user confirms
    // the final result
}

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...