2017-07-26 67 views
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}
+0

你能在什麼_擴大 「但它不工作」 _手段的錯誤?另外,也許發佈你試過的完整代碼? ('//用$ newfile做些事情'表明你已經刪除了你的嘗試?) –

+0

檢查錯誤日誌中的實際錯誤消息。您還可以告訴Slim通過以下設置向您顯示實際的錯誤消息:''displayErrorDetails'=> true',這是您開發時的首選。 –

+0

我休息了這個例子 - > https://akrabat.com/psr-7-file-uploads-in-slim-3/ –

回答

2

我解決了這樣的

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();