Uploaded file already moved

Hi,

Good Day!

Today I’m trying to create a method that will upload a file to a particular folder(upload folder). All are working when my method on uploading is on app\src\routes.php using this code.

$app->post('/upload', function ($request, $response, $args) {
    $files = $request->getUploadedFiles();
    if (empty($files['newfile'])) {
        throw new \Exception('Expected a newfile');
    }
 
    $newfile = $files['newfile'];
    if ($newfile->getError() === UPLOAD_ERR_OK) {
       $uploadFileName = $newfile->getClientFilename();
       $newfile->moveTo(__DIR__. '/../../../uploads/'.$uploadFileName);
    }
});

And I try to move the code to app\src\Controllers\_Controller.php to implement oAuth2 But I unfortunately I got this error.

This is based from this project. slim3-simple-rest-skeleton

Here’s my code:

app\src\routes.php
$app->group(’/upload’, function () {
$this->post(’’, _Controller_oAuth2::class.’:upload’);
});

app\src\Controllers\_Controller_oAuth2.php
public function upload(Request $request, Response $response, $args){

        if ($this->validateToken($request) && isset($this->user)) {
	    	return (parent::upload($request, $response, $args));
    	} else {
            return $response ->withStatus(400);
    	}    
    }

app\src\Controllers\_Controller.php

     public function upload(Request $request, Response $response, $args){
        	
        $this->logger->info(substr(strrchr(rtrim(__CLASS__, '\\'), '\\'), 1).': '.__FUNCTION__);
        
        $files = $request->getUploadedFiles()['newfile'];
       
        if (empty($files)) {
            throw new \Exception('Expected a newfile');
        }

        $uploadFileName = $files->getClientFilename();

        $basepath = __DIR__. '/../../../uploads/'.$uploadFileName;
        
        var_dump($basepath);
        
        if ($files->getError() === UPLOAD_ERR_OK) $files->moveTo($basepath);
    }

Thanks in advance!

I get a same error in slim 4

Hi @HariDev I’d recommend creating a new topic specifically for this issue. When you create the new topic, it would be really helpful if you could provide a detailed description of the problem. Include snippets of the code you’re working with. Also let us know what you’ve attempted so far to solve the issue.

The issue is solved.Thank you