我只是想知道是否有办法限制文件输入对话框只显示某些类型的文件。我的网页只能接受 .bin 或 .gz 文件类型,但用户可以选择其他文件类型并尝试上传它们。
防止上传错误类型文件的最佳方法是什么?
这是我的文件上传控制器:
public function uploadFile()
{
$this->Session->write('isFileUpload', false);
$this->Session->write('isFileLarge', false);
if($this->request->is('post'))
{
$uploadedFile = array();
// Check if the Document object is set
// If it is set, process the file for uploading,
if(isset($this->request->data['Document']))
{
$filename = $this->request->data['Document']['MyFile']['tmp_name'];
$uploadedFile['MyFile']['name'] = $this->request->data['Document']['MyFile']['name'];
$uploadedFile['MyFile']['type'] = $this->request->data['Document']['MyFile']['type'];
$uploadedFile['MyFile']['size'] = $this->request->data['Document']['MyFile']['size'];
// Move the file to the /home/spectracom folder
$filePath = DS . 'home' . DS . $uploadedFile['MyFile']['name'];
if (move_uploaded_file($filename, $filePath))
{
$this->Session->write('isFileUpload', true);
$this->Session->write('isFileLarge', false);
$this->redirect('/tools/upgradebackup');
}
else
{
$this->Session->write('isFileUpload', false);
$this->Session->write('isFileLarge', true);
$this->redirect('/tools/upgradebackup');
}
}
else
{
$this->Session->write('isFileUpload', false);
$this->Session->write('isFileLarge', true);
$this->redirect('/tools/upgradebackup');
}
}
}
我基本上检查文件是否存在,否则它太大,当它返回主升级页面时,它会设置会话变量。
谢谢