dpchen2004 2016-04-03 11:37
浏览 275
已采纳

验证上传的文件扩展名Yii2

I'm trying upload specific files.

This is my code:

[['file'], 'file', 'extensions' => ['pdf','php'], 'wrongExtension'=>'pdf,php files only'],

When i try upload pdf, works great, but when i try upload php file say: "pdf,php files only". (after click in submit button) I tried too, txt, js, etc...and i have the same problem.

print_r($att->file);

yii\web\UploadedFile Object ( [name] => backup-restore-mysql.pdf [tempName] => C:\xampp\tmp\php8364.tmp [type] => application/pdf [size] => 310139 [error] => 0 )
yii\web\UploadedFile Object ( [name] => requirements.php [tempName] => C:\xampp\tmp\phpE22C.tmp [type] => application/octet-stream [size] => 5051 [error] => 0 )

Controller:

if ($att->load(Yii::$app->request->post()) && $att->validate()){
 $att->file = UploadedFile::getInstance($att, 'file');
    if (!empty($att->file)){
        $extension = $att->file->extension;
        $att->Filename = $att->file->baseName;
        $att->Extension = $extension;
        $att->Size = $att->file->size;
        $att->C_Id = $id;

        if ($att->save()){
            if(!is_dir('files/'.$extension.'/')){
                mkdir(('files/'.$extension.'/'));
            }

            $att->file->saveAs('files/'.$extension.'/'.$att->file->name);
        }
    }
}

What i'm doing wrong?

  • 写回答

1条回答 默认 最新

  • duanqun7761 2016-04-04 15:37
    关注

    I solved the question by myself.

    $att->upload_file->saveAs(path,nameOfFile);
    $att->upload_file = NULL;
    $att->save();
    

    Thanks for all replys. @MSost and @G4M1TG

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装