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!