# 使用POST表单上传文件
Files that are uploaded using forms in POST requests can be retrieved with the Request method`getUploadedFiles()`.
> 在POST请求中使用表单上传的文件可以使用请求方法“getuploadedfiles()”检索。
>
When uploading files using a POST request, make sure your file upload form has the attribute`enctype="multipart/form-data"`otherwise`getUploadedFiles()`will return an empty array.
> 当使用POST请求上传文件时,请确保您的文件上传表单具有属性`enctype="multipart/form-data" ' otherwise ' getuploadedfiles()`将返回一个空数组。
If multiple files are uploaded for the same input name, add brackets after the input name in the HTML, otherwise only one uploaded file will be returned for the input name by`getUploadedFiles()`.
> 如果为相同的输入名上传了多个文件,则在HTML中的输入名之后添加括号,否则`getuploadedfiles()`将只返回一个上传的文件作为输入名。
Below is an example HTML form that contains both single and multiple file uploads.
> 下面是一个包含单个和多个文件上传的HTML表单示例。
~~~php
<!-- make sure the attribute enctype is set to multipart/form-data -->
<form method="post" enctype="multipart/form-data">
<!-- upload of a single file -->
<p>
<label>Add file (single): </label><br/>
<input type="file" name="example1"/>
</p>
<!-- multiple input fields for the same input name, use brackets -->
<p>
<label>Add files (up to 2): </label><br/>
<input type="file" name="example2[]"/><br/>
<input type="file" name="example2[]"/>
</p>
<!-- one file input field that allows multiple files to be uploaded, use brackets -->
<p>
<label>Add files (multiple): </label><br/>
<input type="file" name="example3[]" multiple="multiple"/>
</p>
<p>
<input type="submit"/>
</p>
</form>
~~~
Figure 1: Example HTML form for file uploads
> 图1:文件上传的HTML表单示例
> 上传的文件可以移动到一个目录使用`moveTo`'的方法。下面是处理上述HTML表单上传文件的示例应用程序。
>
Uploaded files can be moved to a directory using the`moveTo`method. Below is an example application that handles the uploaded files of the HTML form above.
~~~php
<?php
use DI\Container;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Slim\Factory\AppFactory;
require __DIR__ . '/../vendor/autoload.php';
$container = new Container();
$container->set('upload_directory', __DIR__ . '/uploads');
AppFactory::setContainer($container);
$app = AppFactory::create();
$app->post('/', function(Request $request, Response $response) {
$directory = $this->get('upload_directory');
$uploadedFiles = $request->getUploadedFiles();
// handle single input with single file upload
$uploadedFile = $uploadedFiles['example1'];
if ($uploadedFile->getError() === UPLOAD_ERR_OK) {
$filename = moveUploadedFile($directory, $uploadedFile);
$response->write('uploaded ' . $filename . '<br/>');
}
// handle multiple inputs with the same key
foreach ($uploadedFiles['example2'] as $uploadedFile) {
if ($uploadedFile->getError() === UPLOAD_ERR_OK) {
$filename = moveUploadedFile($directory, $uploadedFile);
$response->write('uploaded ' . $filename . '<br/>');
}
}
// handle single input with multiple file uploads
foreach ($uploadedFiles['example3'] as $uploadedFile) {
if ($uploadedFile->getError() === UPLOAD_ERR_OK) {
$filename = moveUploadedFile($directory, $uploadedFile);
$response->write('uploaded ' . $filename . '<br/>');
}
}
return $response;
});
/**
* Moves the uploaded file to the upload directory and assigns it a unique name
* to avoid overwriting an existing uploaded file.
*
* @param string $directory directory to which the file is moved
* @param UploadedFileInterface $uploaded file uploaded file to move
* @return string filename of moved file
*/
function moveUploadedFile($directory, UploadedFileInterface $uploadedFile)
{
$extension = pathinfo($uploadedFile->getClientFilename(), PATHINFO_EXTENSION);
$basename = bin2hex(random_bytes(8)); // see http://php.net/manual/en/function.random-bytes.php
$filename = sprintf('%s.%0.8s', $basename, $extension);
$uploadedFile->moveTo($directory . DIRECTORY_SEPARATOR . $filename);
return $filename;
}
$app->run();
~~~
> 图2:处理上传文件的瘦应用程序示例
>
Figure 2: Example Slim application to handle the uploaded files
- 开始
- 安装
- 升级指南
- Web服务器
- 概念
- 生命周期
- PSR 7
- 中间件
- 依赖容器
- 实例 及通知和警告处理
- Request
- 请求方法
- 请求头信息
- 请求主体
- 上传的文件
- 请求帮助
- 路由对象
- Response
- 响应状态
- 响应标头
- 响应体
- 返回JSON
- 视图模板
- 路由
- 创建路由
- 路由回调
- 路由策略
- 路线占位符
- 路由名
- 路由组
- 路由中间件
- 路由表达式缓存
- 容器识别解析
- 封装中间件
- 路由的中间件
- 错误处理中间件
- 方法重写的中间件
- 输出缓冲中间件
- 内容长度中间件
- 扩展功能
- 以 / 结尾的路由模式
- 获取当前路由
- 设置CORS
- 使用POST表单上传文件
- 第三方组件
- slim-session
- auth
- slim-api-skeleton
- dir