0
嗨即時通訊新的瘦身3框架(Api爲移動)我想知道如何上傳圖像到服務器,Ë例如沒有找到很多,這是我的代碼但它不起作用。任何幫助將非常感謝。Slim 3上傳圖像到服務器路徑
$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}
你能在什麼_擴大 「但它不工作」 _手段的錯誤?另外,也許發佈你試過的完整代碼? ('//用$ newfile做些事情'表明你已經刪除了你的嘗試?) –
檢查錯誤日誌中的實際錯誤消息。您還可以告訴Slim通過以下設置向您顯示實際的錯誤消息:''displayErrorDetails'=> true',這是您開發時的首選。 –
我休息了這個例子 - > https://akrabat.com/psr-7-file-uploads-in-slim-3/ –