简体   繁体   English

设置文件大小限制消息

[英]Set File Size Limit Message

I wonder whether someone could help me please. 我想知道是否有人可以帮助我。

I have to admit I'm relatively new to writing PHP so please bear with me. 我必须承认我在编写PHP方面还比较陌生,所以请多多包涵。

Through articles I've read on the internet and some first class tutition from one @Marcio on this site, I've put together a script that allows users to save Image Files to a mySQL database. 通过我在互联网上阅读的文章以及该站点上@Marcio的一些一流教学,我编写了一个脚本,该脚本允许用户将图像文件保存到mySQL数据库中。

I've now gone a little further by restricting the size of the file that can be uploaded, but I I'd like to add a warning message that tells why the file cannot be uploaded ie because it's size is greater than the limit set. 现在,我通过限制可以上载的文件的大小进行了进一步的调整,但是我想添加一条警告消息,告诉您为什么无法上载该文件,即因为文件大小大于设置的限制。

I've made an attempt at this, as seen in the code below. 正如下面的代码所示,我已经对此进行了尝试。 But unfortunately I receive an error message stating that there is an unexpected '>' which I know relates to the line I've added, but not sure how to code this another way. 但是不幸的是,我收到一条错误消息,指出有一个意外的“>”,我知道这与我添加的行有关,但不确定如何用另一种方式编码。

Revised Cut Down Code 修改后的缩减代码

 <?php

   // This function makes usage of 
// $_GET, $_POST, etc... variables 
// completly safe in SQL queries 
function sql_safe($s) 
{ 
    if (get_magic_quotes_gpc()) 
        $s = stripslashes($s); 

    return mysql_real_escape_string($s); 
} 

// If user pressed submit in one of the forms 
if ($_SERVER['REQUEST_METHOD'] == 'POST') 
{ 
    if (!isset($_POST["action"])) 
    { 
        // cleaning title field 
        $title = trim(sql_safe($_POST['title'])); 

        if ($title == '') // if title is not set 
            $title = '(No title provided';// use (empty title) string 

        if (isset($_FILES['photo'])) 
        { 
            @list(, , $imtype, ) = getimagesize($_FILES['photo']['tmp_name']); 
            // Get image type. 
            // We use @ to omit errors 

            if ($imtype == 3) // cheking image type 
                $ext="png";   // to use it later in HTTP headers 
            elseif ($imtype == 2) 
                $ext="jpeg"; 
            elseif ($imtype == 1) 
                $ext="gif"; 
            else 
                $msg = 'Error: unknown file format'; 

            if($_FILES["fileupload"]["size"]/1024000 >= 10) // 10mb 
            {     
            $fileErrMsg = "<br />Your uploaded file size:<strong>[ ". $_FILES["fileupload"]["size"]/1024000  . " MB]</strong> is more than allowed 10MB Size.<br />";        

            } 

            if (!isset($msg)) // If there was no error 
            { 
                $data = file_get_contents($_FILES['photo']['tmp_name']); 
                $data = mysql_real_escape_string($data); 
                // Preparing data to be used in MySQL query 

                mysql_query("INSERT INTO {$table} 
                                SET ext='$ext', title='$title', 
                                    data='$data'"); 

                $msg = 'Success: Image Uploaded'; 
            } 
        } 

I just wondered whether someone could perhaps take a look at this and let me know what I'm doing wrong. 我只是想知道是否有人可以看一下这个,然后让我知道我做错了什么。

Many thanks and kind regards 非常感谢和问候

getfilesize() returns image dimensions in pixels, not file size. getfilesize()返回图像尺寸(以像素为单位),而不是文件尺寸。 You need something along the lines of this: 您需要遵循以下要求:

if (filesize($_FILES['tmp_name']) >= 100000)

You can use this 你可以用这个

if($_FILES["fileupload"]["size"]/1024000 >= 10) // 10mb
{
    $fileErrMsg = "<br />Your uploaded file size:<strong>[ ". $_FILES["fileupload"]["size"]/1024000  . " MB]</strong> is more than allowed 10MB Size.<br />";   
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM