在php上传后损坏文件

时间:2015-08-25 16:17:06

标签: php file-upload upload

我为上传文件编写了php脚本。它似乎工作正常,但在上传文件夹中打开上传文件时,我收到此错误: 格式未知或已损坏 我的代码:

HTML:

    <form method="POST" action="<?=URL?>user/homework" enctype="multipart/form-data">
    <input type="hidden" id="csrf" name="csrf" value="<?= $_SESSION['csrf'] ?>" />
<table class="table table-responsive">
    <tr>
        <td><label>file</label></td>
        <td><input multiple="multiple" type="file" name="file"/></td>
    </tr>
</table>
    <input type="submit" name="sub" value="submit" class="btn btn-success"/>
</form>

PHP:

if (isset($_POST['sub'])) {
            if (Security::chekCsrf($_POST['csrf']) == FALSE) {
                $data = array('', 'dan');
                $this->render('homework', $data);
                exit();
            }
            if (!empty($_FILES['file'])) {
                $name = time() . '_' . $_FILES['file']['name'];
                if ($_FILES['file']['size'] > 20971520) {
                    $data = array('size high', 'war');
                    $this->render('homework', $data);
                    exit();
                }
                $valid = array('application/octet-stream', 'application/x-rar-compressed', 'application/zip');
                $type = $_FILES['file']['type'];
                if (!in_array($type, $valid)) {
                    $data = array('wrong format', 'war');
                    $this->render('homework', $data);
                    exit();
                }
                $dir = 'upload/' . $name;
                if (move_uploaded_file($_FILES['file']['tmp_name'], $dir)) {
                    $data = array('ok', 'suc');
                    $this->render('homework', $data);
                    exit();
                } else {
                    $data = array('problem', 'dan');
                    $this->render('homework', $data);
                    exit();
                }
            }
        }

0 个答案:

没有答案