php中的文件大小错误

时间:2016-05-09 04:09:08

标签: php

我正在验证php中的文件上传。我想验证文件

if > 3MB: display error

else: proceed to upload

我现在正在做的是,如果文件大小大于或小于3 MB,则显示错误。以下是代码:

/*
* other validation of post here
*
*/
if (isset($_FILES['uploaded'])) {
    $filesize = $_FILES['uploaded']['size']; 
    if ($filesize > 3145728) {
        $msgError[] = 'File can not be more than 3MB';  
    }
    for ($i=0; $i < count($_FILES['uploaded']['name']); $i++) {
        $tmpFilePath = $_FILES['uploaded']['tmp_name'][$i];
        if (!empty($tmpFilePath)) {
            $shortname = $_FILES['uploaded']['name'][$i];
            $filePath = "uploads/" . date('d-m-Y-H-i-s').'-'.$_FILES['uploaded']['name'][$i];
            if (!$msgError && move_uploaded_file($tmpFilePath, $filePath)) {
                $sql = " UPDATE purchase SET quotation_1='".date('d-m-Y-H-i-s').'-'.$_FILES['uploaded']['name'][0]."', quotation_2='".date('d-m-Y-H-i-s').'-'.$_FILES['uploaded']['name'][1]."', quotation_3='".date('d-m-Y-H-i-s').'-'.$_FILES['uploaded']['name'][2]."' 
                WHERE pr_id='$pr_id' ";
                $result = mysqli_query($conn, $sql);
                if(mysqli_query($conn, $sql)){
                    mysqli_close($conn); 
                    $pr_idEnc = urlencode(encryptor('encrypt', $pr_id)); 
                    header("Location: ".BASE_PATH."update-purchase.php?success&pr_id=".$pr_idEnc."");
                } else {
                    mysqli_close($conn); 
                    echo "Error updating record: " . mysqli_error($conn);
                }
            } else {
                $msgErrorString = implode(",",$msgError); 
                $pr_idEnc = urlencode(encryptor('encrypt', $pr_id)); 
                mysqli_close($conn);
                header("Location: ".BASE_PATH."update-purchase.php?pr_id=".$pr_idEnc."&msgError=".$msgErrorString."");
            } 
        }
    }
}

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

为什么不更改upload_max_filesize文件中的upload_max_filesizephp.ini设置?

upload_max_filesize  = 3 M
post_max_size = 3 M

<强>的post_max_size

  

设置允许的帖子数据的最大大小。此设置也会影响文件上载。要上传大文件,此值必须大于   upload_max_filesize的。一般来说,memory_limit应该更大   比post_max_size。使用整数时,将测量该值   字节。

<强>的upload_max_filesize

  

上传文件的最大大小。当使用整数时,   值以字节为单位。