Api upload a file

Hello,

i’m new to slim, i’m working on a project where i need to register , retrieve clients et , i’v done that all good but now i’m trying to manage files upload , just to be clear i am consuming the api with another php application.

so for now i got this error :

Details
Type: Error
Message: Call to a member function getError() on null
File: C:\xampp\htdocs\API_supfile\index.php
Line: 82
Trace
#0 [internal function]: Closure->{closure}(Object(Slim\Http\Request), Object(Slim\Http\Response), Array)
#1 C:\xampp\htdocs\API_supfile\vendor\slim\slim\Slim\Handlers\Strategies\RequestResponse.php(41): call_user_func(Object(Closure), Object(Slim\Http\Request), Object(Slim\Http\Response), Array)
#2 C:\xampp\htdocs\API_supfile\vendor\slim\slim\Slim\Route.php(335): Slim\Handlers\Strategies\RequestResponse->__invoke(Object(Closure), Object(Slim\Http\Request), Object(Slim\Http\Response), Array)
#3 C:\xampp\htdocs\API_supfile\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(117): Slim\Route->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response))
#4 C:\xampp\htdocs\API_supfile\vendor\slim\slim\Slim\Route.php(313): Slim\Route->callMiddlewareStack(Object(Slim\Http\Request), Object(Slim\Http\Response))
#5 C:\xampp\htdocs\API_supfile\vendor\slim\slim\Slim\App.php(513): Slim\Route->run(Object(Slim\Http\Request), Object(Slim\Http\Response))
#6 C:\xampp\htdocs\API_supfile\src\spfile\middleware\logging.php(11): Slim\App->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response))
#7 [internal function]: spfile\middleware\Logging->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Slim\App))
#8 C:\xampp\htdocs\API_supfile\vendor\slim\slim\Slim\DeferredCallable.php(43): call_user_func_array(Object(spfile\middleware\Logging), Array)
#9 [internal function]: Slim\DeferredCallable->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Slim\App))
#10 C:\xampp\htdocs\API_supfile\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(70): call_user_func(Object(Slim\DeferredCallable), Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Slim\App))
#11 C:\xampp\htdocs\API_supfile\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(117): Slim\App->Slim{closure}(Object(Slim\Http\Request), Object(Slim\Http\Response))
#12 C:\xampp\htdocs\API_supfile\vendor\slim\slim\Slim\App.php(406): Slim\App->callMiddlewareStack(Object(Slim\Http\Request), Object(Slim\Http\Response))
#13 C:\xampp\htdocs\API_supfile\vendor\slim\slim\Slim\App.php(314): Slim\App->process(Object(Slim\Http\Request), Object(Slim\Http\Response))
#14 C:\xampp\htdocs\API_supfile\index.php(105): Slim\App->run()
#15 {main}

This is the code:

$app->post(’/photo’, function (Request $request, Response $response) {

    $directory = $this->get('upload_directory');

echo 'hi ';
$uploadedFiles = $request->getUploadedFiles();

    $uploadedFile = $uploadedFiles['picture'];
    if($uploadedFile->getError() === UPLOAD_ERR_OK) {
        $filename = moveUploadedFile($directory, $uploadedFile);
        $response->write('uploaded ' . $filename . '<br/>');
    }

});

function moveUploadedFile($directory, UploadedFile $uploadedFile){
$extension = pathinfo($uploadedFile->getClientFilename(),
PATHINFO_EXTENSION);
$basename = bin2hex(random_bytes(8));
$filename = sprintf(’%s.%0.8s’, $basename, $extension);
$uploadedFile->moveTo($directory . DIRECTORY_SEPARATOR . $filename);

return $filename;

}

The error is telling you that variable $uploadedFile is null. If you are uploading files means that html form isn’t sending files.