XF 2.1 Help with upload form

grantus

Active member
I'm trying to get an upload form to work but encountering some issues. First of all, here's my code so far in my Controller:


Code:
public function actionUpload(ParameterBag $params)
{
    $dataDir = \XF::app()->config('externalDataPath');
    $upload = $this->request->getFile('upload', false, false);

    $originalFileName = '';

    if ($upload && strtolower($upload->getExtension()) === 'mp3') 
    {
        $originalFileName = $upload->getFileName();

        $targetDirectory = $dataDir . '://upload_test/';

        $dataDir = $targetDirectory . $originalFileName;

        \XF\Util\File::copyFileToAbstractedPath($upload->getTempFile(), $dataDir);
        return true;
    }

    $viewParams = [
        'upload' => $upload,
        'dataDir' => $dataDir
    ];

    return $this->view('Testing\Test:Upload', 'upload_test', $viewParams);
}

I have everything set up properly with routing.

When I submit the form, it moves my mp3 file to the proper directory but then I get this error message:

Code:
TypeError: XF\Mvc\Controller::postDispatch(): Argument #3 ($reply) must be of type XF\Mvc\Reply\AbstractReply, bool given

Does anyone know what the issue is?

Thanks.
 
You can't simply return true...you need to return some kind of controller reply. You could create a new page that says something about a successful upload, or redirect to the page that views the uploaded file.
 
You can't simply return true...you need to return some kind of controller reply. You could create a new page that says something about a successful upload, or redirect to the page that views the uploaded file.
This is what I want to do so I figured the error was for a lack of reply. I just don't know how to proceed with a reply where it goes to a new page to show the success and uploaded file.
 
Last edited:
You could replace return true; with something like return $this->redirect($this->buildLink('upload/viewer'));.

Of course you'd want to replace upload/viewer with the route that is used to view the uploaded files.

You could also just return a page directly from there that only displays the uploaded file...something like this might work:

PHP:
if ($upload && strtolower($upload->getExtension()) === 'mp3') 
{
    $originalFileName = $upload->getFileName();

     $targetDirectory = $dataDir . '://upload_test/';

    $dataDir = $targetDirectory . $originalFileName;

    \XF\Util\File::copyFileToAbstractedPath($upload->getTempFile(), $dataDir);
    
    $viewParams = [
        'uploadedFile' => $dataDir
    ];
    return $this->view('Testing\Test:Upload', 'addon_upload_success', $viewParams);
}

Then you'd want to create a template called addon_upload_success that will display the uploaded file that's found in {$uploadedFile}.
 
You could replace return true; with something like return $this->redirect($this->buildLink('upload/viewer'));.

Of course you'd want to replace upload/viewer with the route that is used to view the uploaded files.

You could also just return a page directly from there that only displays the uploaded file...something like this might work:

PHP:
if ($upload && strtolower($upload->getExtension()) === 'mp3')
{
    $originalFileName = $upload->getFileName();

     $targetDirectory = $dataDir . '://upload_test/';

    $dataDir = $targetDirectory . $originalFileName;

    \XF\Util\File::copyFileToAbstractedPath($upload->getTempFile(), $dataDir);
   
    $viewParams = [
        'uploadedFile' => $dataDir
    ];
    return $this->view('Testing\Test:Upload', 'addon_upload_success', $viewParams);
}

Then you'd want to create a template called addon_upload_success that will display the uploaded file that's found in {$uploadedFile}.
Okay now I understand. That makes sense to just send it to a new success page.

Thanks for the tip!
 
Back
Top Bottom