Slim 3上传图片到服务器路径

Slim 3 upload image to server path

你好,我是 slim 3 框架的新手(Api 移动版)我想知道如何将图像上传到服务器,例如 e seacrh 没找到多少,这是我的代码但是没有 work.Any 非常感谢。

  $app->post('/photo', function ($request, $response) use ($app) {

   $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("../photos/");
   }

 });

它给我的错误

 <h1>Slim Application Error</h1>
    <p>The application could not run because of the following error:</p>
    <h2>Details</h2>
    <div>
        <strong>Type:</strong> Error
    </div>
    <div>
        <strong>Message:</strong> Call to a member function getError() on null
    </div>
    <div>
        <strong>File:</strong> /home/bitstudi/public_html/api_pricegram/public/index.php
    </div>
    <div>
        <strong>Line:</strong> 155
    </div>
    <h2>Trace</h2>

#0 [internal function]: Closure->{closure}(Object(Slim\Http\Request), Object(Slim\Http\Response), Array)
#1 /home/bitstudi/public_html/api_pricegram/vendor/slim/slim/Slim/Handlers/Strategies/RequestResponse.php(41): call_user_func(Object(Closure), Object(Slim\Http\Request), Object(Slim\Http\Response), Array)

#2 /home/bitstudi/public_html/api_pricegram/vendor/slim/slim/Slim/Route.php(344): Slim\Handlers\Strategies\RequestResponse->__invoke(Object(Closure), Object(Slim\Http\Request), Object(Slim\Http\Response), Array)

#3 /home/bitstudi/public_html/api_pricegram/vendor/slim/slim/Slim/MiddlewareAwareTrait.php(122): Slim\Route->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response))

#4 /home/bitstudi/public_html/api_pricegram/vendor/slim/slim/Slim/Route.php(316): Slim\Route->callMiddlewareStack(Object(Slim\Http\Request), Object(Slim\Http\Response))

#5 /home/bitstudi/public_html/api_pricegram/vendor/slim/slim/Slim/App.php(476): Slim\Route->run(Object(Slim\Http\Request), Object(Slim\Http\Response))

#6 /home/bitstudi/public_html/api_pricegram/vendor/slim/slim/Slim/MiddlewareAwareTrait.php(122): Slim\App->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response))

#7 /home/bitstudi/public_html/api_pricegram/vendor/slim/slim/Slim/App.php(370): Slim\App->callMiddlewareStack(Object(Slim\Http\Request), Object(Slim\Http\Response))

#8 /home/bitstudi/public_html/api_pricegram/vendor/slim/slim/Slim/App.php(295): Slim\App->process(Object(Slim\Http\Request), Object(Slim\Http\Response))

#9 /home/bitstudi/public_html/api_pricegram/public/index.php(161): Slim\App->run()

#10 {main}

我是这样解决的

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
use Slim\Http\UploadedFile;

require '../vendor/autoload.php';

$app = new \Slim\App([
'settings' => [
  'displayErrorDetails' => true
]
  ]);



$container = $app->getContainer();
$container['upload_directory'] ='../photos/';

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

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

$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;
}


$app->run();