Filesize错误信息


Filesize Error Message

不知是否有人能帮我一下。

我正在尝试将'filesize'错误信息合并到脚本中,如下所示,用于将BLOB文件上传到mySQL服务器。

if (!mysql_connect($db_host, $db_user, $db_pwd))
    die("Can't connect to database");
if (!mysql_select_db($database))
    die("Can't select database");
// 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 
            @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) 
            {     
            $fileErrMsg = "<br />Your uploaded file size:<strong>[ ". $_FILES["fileupload"]["size"]/1024000  . " MB]</strong> is more than allowed Size.<br />";        
            } 
        if (isset($_FILES['photo'])) 
        { 
            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'; 
            } 
        } 
        elseif (isset($_GET['title']))      // isset(..title) needed 
            $msg = 'Error: file not loaded';// to make sure we've using 
                                            // upload form, not form 
                                            // for deletion  
        if (isset($_POST['del'])) // If used selected some photo to delete 
        {                         // in 'uploaded images form'; 
            $imageid = intval($_POST['del']); 
            mysql_query("DELETE FROM {$table} WHERE imageid=$imageid"); 
            $msg = 'Image deleted'; 
        } 
        if (isset($_POST['view'])) // If used selected some photo to delete  
        { // in 'uploaded images form';  
            $imageid = intval($_POST['view']);  
            mysql_query("SELECT ext, data FROM {$table} WHERE imageid=$imageid");  
            if(mysql_num_rows($result) == 1)  
            {  
                $image = $row['myimage'];  
                header("Content-type: image/gif"); // or whatever  
                print $image;  
                exit;  
            }  
        }  
    } 
    else 
    { 
        $imageid = intval($_POST['del']); 
        if ($_POST["action"] == "view") 
        { 
            $result = mysql_query("SELECT ext, UNIX_TIMESTAMP(imagetime), data 
                                     FROM {$table} 
                                    WHERE imageid=$imageid LIMIT 1"); 
            if (mysql_num_rows($result) == 0) 
                die('no image'); 
            list($ext, $imagetime, $data) = mysql_fetch_row($result); 
            $send_304 = false; 
            if (php_sapi_name() == 'apache') { 
                // if our web server is apache 
                // we get check HTTP 
                // If-Modified-Since header 
                // and do not send image 
                // if there is a cached version 
                $ar = apache_request_headers(); 
                if (isset($ar['If-Modified-Since']) && // If-Modified-Since should exists 
                    ($ar['If-Modified-Since'] != '') && // not empty 
                    (strtotime($ar['If-Modified-Since']) >= $imagetime)) // and grater than 
                    $send_304 = true;                                     // imagetime 
            } 
            if ($send_304) 
            { 
                // Sending 304 response to browser 
                // "Browser, your cached version of image is OK 
                // we're not sending anything new to you" 
                header('Last-Modified: '.gmdate('D, d M Y', $ts).' GMT', true, 304); 
                exit(); // bye-bye 
            } 
            // outputing HTTP headers 
            header('Content-Length: '.strlen($data)); 
            header("Content-type: image/{$ext}"); 
            // outputing image 
            echo $data; 
            exit(); 
        } 
        else if ($_POST["action"] == "delete") 
        { 
            $imageid = intval($_POST['del']); 
            mysql_query("DELETE FROM {$table} WHERE imageid=$imageid"); 
            $msg = 'Image deleted'; 
        } 
    } 
} 
?> 

通过我在这个网站上收到的一些指导,我已经能够想出检查文件大小的方法,从这一行开始:

if($_FILES["fileupload"]["size"]/1024000 >= 10) 

但是我不能使错误信息工作。

如果文件大小超过1MB,则需要激活特定的消息。当我尝试上传大于此值的文件时,文件被正确拒绝,但我收到不正确的错误消息,' error: unknown file format'。

我已经尝试了所有的方法来尝试让这个工作,但我只是得到同样不正确的错误信息。

如果有人能看一下这个,告诉我哪里错了,我将非常感激。

多谢<<p> 解决方案/strong>

if (isset($_FILES['photo'])) 
{ 
list($width, $height, $imtype, $attr) = getimagesize($_FILES['photo']['tmp_name']);
// Get image type. 
if ($imtype == 3) 
$ext="png"; // 
elseif ($imtype == 2) 
$ext="jpeg"; 
elseif ($imtype == 1) 
$ext="gif"; 
else 
$msg = 'Error: unknown file format'; 
if($_FILES["photo"]["size"]/102400 >= 1)  {          
$msg = "he file you wish to upload is:<strong>[ ". $_FILES["photo"]["size"]/1024000  . " MB]</strong> is more than allowed Size.";         
} 

我是php新手,但我搜索了一下,发现这个在手册

http://php.net/manual/en/function.set-error-handler.php

我张贴了一个答案,因为我不能评论。

看起来所有其他错误消息都放入一个名为$msg的变量中。我更新了你的计算更容易一点:

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