我有一个可以将文件上传到服务器的表单。我在 .aes 文件(mimeType:application/x-aes-encrypted)上出错(系统无法检测此文件的 mimeType),其他文件格式工作正常。我正在使用 PHP 8.0.
已在 PHP 8.1 上完成测试并能够上传 .aes 文件。
为什么它在 PHP 8.0 上出错?
错误触发器:
Array ( [fileUpload] => Array ( [0] => Only files with these MIME types are allowed: application/pdf, image/jpeg, image/png, text/plain, text/csv, text/tsv, application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/msword, application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/zip, application/vnd.rar, application/x-aes-encrypted. ) )
以下是上传表格:
<?php $form = ActiveForm::begin([
'id' => 'upload-form',
'options' => ['enctype' => 'multipart/form-data'],
'action' => ['create', 'client_id' => $model->client_id]
]); ?>
<div class="well">
<div class="row">
<div class="col-md-6">
<?= $form->field($model, 'client_id')
->dropDownList(
$clients,
['prompt' => Yii::t('app', '-- Select --')]
);
?>
</div>
<div class="col-md-6">
<?= $form->field($model, 'files[]')->fileInput(['multiple' => true]) ?>
</div>
</div>
</div>
<div class="form-group">
<?= Html::submitButton(
'<i class="fa fa-check fa-fw"></i> ' . Yii::t('app', 'Submit'),
['class' => 'btn btn-sm btn-success']
); ?>
</div>
<?php ActiveForm::end(); ?>
上传控制器:
public function actionCreate(int $client_id)
{
if (!$client = Client::findOne($client_id)) {
throw new NotFoundHttpException();
}
$clientCodes = array_keys(Yii::$app->params['sftp']);
if (!in_array(strtolower($client->code), $clientCodes)) {
throw new NotFoundHttpException();
}
$model = new SftpForm([
'client_id' => $client_id
]);
$clients = Client::getCachedList(
'Clients',
'code',
['IN', 'lower(client.code)', $clientCodes]
);
if (Yii::$app->request->isPost) {
$model->load(Yii::$app->request->post());
if (is_uploaded_file($_FILES['SftpForm']['tmp_name']['files'][0])) {
$model->files = UploadedFile::getInstances($model, 'files');
$error_message = [];
foreach ($model->files as $fileInfo) {
$attachment = new Attachment([
'fileExtensions' => ['png', 'jpg', 'jpeg', 'pdf', 'xls', 'xlsx', 'txt', 'doc', 'docx', 'zip', 'rar', 'aes', 'csv'],
'checkExtensionByMimeType' => false,
'mimeTypes' => [
'application/pdf',
'image/jpeg',
'image/png',
'text/plain',
'text/csv',
'text/tsv',
'application/vnd.ms-excel',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'application/msword',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'application/zip',
'application/vnd.rar',
'application/x-aes-encrypted',
],
'fileUpload' => [$fileInfo],
'model_name' => 'Client',
'object_id' => $client->id,
'active' => false,
]);
$attachment->setFileInfo();
$jsonData = ['sftp' => true];
$attachment->json_data = json_encode($jsonData);
if (!$attachment->upload() || !$attachment->save(false)) {
$error_message[] = Yii::t('app', $attachment->original_name);
}
}
$message = Yii::t('app', 'File upload is successfully completed.');
if ($error_message) {
$error_files = implode(',', $error_message);
$message = $message . Yii::t('app', ' Error on files : {error_files}', ['error_files' => $error_files]);
}
Yii::$app->session->setFlash('success', $message);
return $this->redirect([
'client/view',
'id' => $model->client_id,
'active-tab' => 'sftp_upload'
]);
}
}
return $this->render($this->masterCreate, [
'model' => $model,
'clients' => $clients,
]);
}