在 Slim 框架中移动上传的文件时出错



>我正在尝试使用苗条框架上传文件,但我不断收到这个烦人的错误 - '"将上传的文件转储.jpg移动到/var/www/html/user-name/src/uploads 时出错">

这是我的代码:

$app->post('/issues/create', function( Request $request, Response $response, array $args ) {
$files = $request->getUploadedFiles();
$file  = $files['photo'];
$destinationPath = __DIR__.'/uploads';
if( $file->getError() === UPLOAD_ERR_OK ) {
$file->moveTo($destinationPath);
}
});

请问我做错了什么?

代码本身看起来正确,但错误消息不是最佳的,因为它没有指定在尝试移动文件时出了什么问题。我认为这是所有权/权限问题,这些是此类错误的最常见原因。

你能检查运行你的应用程序的用户(可能是www-data(是否被允许在/var/www/html/safeMonkeyApi/src/uploads内创建新文件吗?

与错误无关,您可能应该在使用之前检查是否设置了$files['photo']。永远不要相信用户输入。;-)

指定新文件的名称,如下所示:

$uploadPath = $_SERVER['DOCUMENT_ROOT'] . '/uploads/';
$uploadedFiles = $request->getUploadedFiles();
$file = $uploadedFiles['file'];
$file->moveTo($uploadPath . $file->getClientFilename());

最新更新