PHP parse SDK上传文件时如何限制文件大小?

How can I limit the file size when I try to upload files via PHP parse SDK?

我正在尝试将文件上传到 parse SDK 云,从技术上讲它可以工作,但现在我想限制用户能够上传的最大文件大小。这是代码:

if ( isset( $_FILES['image'] ) ) {

    // save file to Parse
    $file = ParseFile::createFromData( file_get_contents( $_FILES['image']['tmp_name'] ), $_FILES['image']['name']  );
    $file->save();
}

如果您查看 $_FILES 数组,您会发现每个数组都传递了一段名为 $_FILES['image']['size']

的数据

如果您想限制 PHP 端的大小,那么只需测试该字段

if ( isset( $_FILES['image'] ) ) {
    if ( $_FILES['image']['size'] < 10000 ) {
        // save file to Parse
        $file = ParseFile::createFromData( 
                   file_get_contents( $_FILES['image']['tmp_name'] ), 
                   $_FILES['image']['name']  );
        $file->save();
     } else {
         // return some sort of error
     }
}

记住$_FILES['image']['size']是以BYTES记录的。

试试这个方法,

if(isset($_FILES['image']) {
if($_FILES['image']['size'] > 10485760) { //10 MB (size is also in bytes)
    // File too big
} else {
     $file = ParseFile::createFromData( 
               file_get_contents( $_FILES['image']['tmp_name'] ), 
               $_FILES['image']['name']  );
     $file->save();
}
}

只需使用 if 语句并获取文件的大小

$_FILES['image']['size']
//The size, in bytes, of the uploaded file.

类似

if($_FILES['image']['size'] < 10000000)
{
    // save file to Parse
    $file = ParseFile::createFromData( file_get_contents( $_FILES['image' ['tmp_name'] ), $_FILES['image']['name']  );
    $file->save();
}
else
{
    //Tell user that the file is too big, however you prefer
}

客户端内容: 直接来自 PHP 文档:

<!-- The data encoding type, enctype, MUST be specified as below -->
<form enctype="multipart/form-data" action="__URL__" method="POST">
    <!-- MAX_FILE_SIZE must precede the file input field -->
    <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
    <!-- Name of input element determines name in $_FILES array -->
    Send this file: <input name="userfile" type="file" />
    <input type="submit" value="Send File" />
</form>

The URL in the above example should be replaced, and point to a PHP file.

The MAX_FILE_SIZE hidden field (measured in bytes) must precede the file input field, and its value is the maximum filesize accepted by PHP. This form element should always be used as it saves users the trouble of waiting for a big file being transferred only to find that it was too large and the transfer failed. Keep in mind: fooling this setting on the browser side is quite easy, so never rely on files with a greater size being blocked by this feature. It is merely a convenience feature for users on the client side of the application. The PHP settings (on the server side) for maximum-size, however, cannot be fooled.

您可以在解析文件之前限制文件大小。见下文:

    if ( isset( $_FILES['image'] ) ) {
        $maxsize    = 2097152; // 2MB 

        if(($_FILES['image']['size'] >= $maxsize) || ($_FILES["image"]["size"] == 0)) {
            $message = 'File too large. File must be less than 2 megabytes.'; 
            echo '<script type="text/javascript">alert("'.$message.'");</script>'; 
        }

        // save file to Parse
        $file = ParseFile::createFromData( file_get_contents( $_FILES['image']['tmp_name'] ), $_FILES['image']['name']  );
        $file->save();
    }