cakephp - 无法让 CakePHP 3.8 多文件上传工作
问题描述
我正在做这个项目,起初它有一个非常好的单个文件上传,但不得不更改为多个文件上传,现在我无法让它工作!收到“标题已发送”错误:
Deprecated (16384): Accessing `here` as a property will be removed in 4.0.0. Use request->getAttribute("here") instead. - /home1/thecyb80/seibo.the-cyberpunk.com/src/Controller/AppController.php, line: 92
You can disable deprecation warnings by setting `Error.errorLevel` to `E_ALL & ~E_USER_DEPRECATED` in your config/app.php. [CORE/src/Core/functions.php, line 311]
Warning (512): Unable to emit headers. Headers sent in file=/home1/thecyb80/seibo.the-cyberpunk.com/vendor/cakephp/cakephp/src/Error/Debugger.php line=856 [CORE/src/Http/ResponseEmitter.php, line 51]
Warning (2): Cannot modify header information - headers already sent by (output started at /home1/thecyb80/seibo.the-cyberpunk.com/vendor/cakephp/cakephp/src/Error/Debugger.php:856) [CORE/src/Http/ResponseEmitter.php, line 152]
Warning (2): Cannot modify header information - headers already sent by (output started at /home1/thecyb80/seibo.the-cyberpunk.com/vendor/cakephp/cakephp/src/Error/Debugger.php:856) [CORE/src/Http/ResponseEmitter.php, line 185]
Warning (2): Cannot modify header information - headers already sent by (output started at /home1/thecyb80/seibo.the-cyberpunk.com/vendor/cakephp/cakephp/src/Error/Debugger.php:856) [CORE/src/Http/ResponseEmitter.php, line 185]
这是我的代码:add.ctp
<?= $this->Form->create($atividade, ['type' => 'file']) ?>
[...]
<div class="form-group m-0">
<label class="btn btn-light p-3 rounded border d-flex align-items-center justify-content-center flex-column" style="cursor: pointer; min-height: 350px;">
<strong class="d-block">Selecione o(s) arquivo(s) da atividade. Você pode enviar até 10 arquivos.</strong>
<span class="exercicio-nome d-flex aling-items-center justify-content-center w-100 mt-3"></span>
<?= $this->Form->file('arquivos[]', [ 'class' => 'exercicio-item', 'multiple' => 'multiple', 'style' => 'display: none;', 'label' => [ 'class' => 'd-none' ] ]) ?>
</label>
</div>
验证成立于AtividadesTable.php
(这是该字段的唯一出现):
$validator
->scalar('arquivos')
->maxLength('arquivos', 255)
->allowEmptyString('arquivos')
->allowEmptyFile('arquivos');
我尝试了几种解决方案,我在互联网上找到了这些解决方案,但没有一个对我有帮助。我在 PHP 7.2.31 上使用 CakePHP 3.8.11。
这是我composer.json
安装的模块(不知道是否有帮助):
"require": {
"php": ">=5.6",
"cakephp/cakephp": "3.8.*",
"cakephp/migrations": "^2.0.0",
"cakephp/plugin-installer": "^1.0",
"mobiledetect/mobiledetectlib": "2.*"
}
由于我无法完成这一步,我假设现在,我的控制器是无关紧要的。我能做些什么?
编辑:06/04(由Salines询问)
多文件上传(上传4个文件)
使用
<?= $this->Form->file('arquivos[]', [ 'class' => 'exercicio-item', 'multiple' => 'multiple', 'style' => 'display: none;', 'label' => [ 'class' => 'd-none' ] ]) ?>
输出
(Page just refreshs)
单个文件上传
使用
<?= $this->Form->file('arquivos[]', [ 'class' => 'exercicio-item', 'multiple' => 'multiple', 'style' => 'display: none;', 'label' => [ 'class' => 'd-none' ] ]) ?>
输出
(Page just refreshs)
多文件上传(上传4个文件)
使用
<?= $this->Form->file('arquivos', [ 'class' => 'exercicio-item', 'multiple' => 'multiple', 'style' => 'display: none;', 'label' => [ 'class' => 'd-none' ] ]) ?>
输出
/src/Controller/AtividadesController.php (line 159)
[
'nome' => 'Atividade teste',
'status' => '1',
'users' => [
'_ids' => [
(int) 0 => '306'
]
],
'turmas' => [
'_ids' => [
(int) 0 => '17'
]
],
'materias' => [
'_ids' => [
(int) 0 => '45'
]
],
'descricao' => '<p>Teste</p>
',
'comeca_dia' => [
'year' => '2020',
'month' => '06',
'day' => '04'
],
'termina_dia' => [
'year' => '2020',
'month' => '06',
'day' => '04'
],
'video' => '',
'aviso_do_professor' => 'Teste',
'arquivos' => [
'tmp_name' => '/tmp/phpafKkWr',
'error' => (int) 0,
'name' => 'imagem_sobre.png',
'type' => 'image/png',
'size' => (int) 600995
]
]
多文件上传(上传4个文件)
使用
<?= $this->Form->file('arquivos. ', [ 'class' => 'exercicio-item', 'multiple' => 'multiple', 'style' => 'display: none;', 'label' => [ 'class' => 'd-none' ] ]) ?>
输出
(Page just refreshs)
Atividades控制器
我将展示我的AtividadesController.php
(添加方法):
/**
* Add method
*
* @return \Cake\Http\Response|null Redirects on successful add, renders view otherwise.
*/
public function add($nivelId = null)
{
$atividade = $this->Atividades->newEntity();
if ($this->request->is('post')) {
// Edit - StackOverflow
debug($this->getRequest()->getData()); exit;
$atividade = $this->Atividades->patchEntity($atividade, $this->request->getData());
die(debug($_FILES));
// Salvando os arquivos
if (!empty($_FILES['arquivos']['name'])) {
$file = $_FILES['arquivos'];
$arquivoName = $file['name'];
$userId = $this->Auth->user('id');
$ext = substr(strtolower(strrchr($arquivoName, '.')), 1);
$arr_ext = array('zip', 'rar', 'pdf', 'doc', 'docx', 'ppt', 'pptx');
if (in_array($ext, $arr_ext)) {
$imgHashed = md5($arquivoName) . time() . '.' . $ext;
$fileDir = WWW_ROOT . 'files/' . $userId;
if (!is_dir($fileDir)) {
mkdir($fileDir);
}
@move_uploaded_file($file['tmp_name'], $fileDir . '/' . $imgHashed);
$atividade['arquivos'] = $imgHashed;
$atividade['arquivo_nome'] = $arquivoName;
} else {
$this->Flash->error(__("Só aceitamos imagens dos tipos 'zip', 'rar', 'pdf', 'doc', 'docx', 'ppt' ou 'pptx'."));
return $this->redirect(['action' => 'add']);
}
}
if ($this->Atividades->save($atividade)) {
$this->Flash->success(__('A atividade foi salva.'));
return $this->redirect(['action' => 'index', $this->Auth->user('id')]);
}
$this->Flash->error(__('A atividade não pode ser salva. Por favor, tente novamente.'));
}
$arquivos = $this->Atividades->Arquivos->find('list');
$materias = $this->Atividades->Materias->find('list');
if ($nivelId) {
$turmas = $this->Atividades->Turmas->find('list')->matching('Nivels', function($q) use ($nivelId) {
return $q
->select()
->where(['Nivels.id' => $nivelId]);
});
} else {
$turmas = $this->Atividades->Turmas->find('list');
}
$users = $this->Atividades->Users->find('list')->where(['tipo' => 'professor']);
$this->set(compact('atividade', 'arquivos', 'materias', 'turmas', 'users'));
}
我错过了什么吗?一些 CakePHP 组件,也许吧?一些 PHP 扩展?
解决方案
推荐阅读
- typescript - 如何使用打字稿在 d3 svg 上正确键入缩放?
- mongodb - 有没有更简单的方法/工具来查询 mongo 文档?
- azure-web-app-service - Azure Web App Windows 容器在正常关闭之前终止
- information-retrieval - 信息检索、精度和召回python
- json - 如何将 JSON 作为字符串插入另一个 JSON
- javascript - EcmaScript 中的调用表达式使用什么语法?
- sharepoint - 如何更改 SharePoint 设置以使其使用 AG 侦听器而不是特定的 DB 服务器?
- regex - 在分号之前选择的正则表达式
- node.js - 使用 OAuth2 nodejs 获取 Google Cloud Platform 中的用户项目列表
- javascript - React 上的“TypeError:无法读取未定义的属性‘状态’”