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

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

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法