2011-12-15 66 views
0

我想知道是否有人可以幫助我。文件大小錯誤信息

我試圖將一個'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,則需要激活特定消息。當我嘗試上傳比此更大的文件時,該文件被正確拒絕,但我收到了不正確的錯誤消息'錯誤:未知文件格式'。

我已經嘗試了所有方法來試圖讓這個工作,但我只是得到相同的不正確的錯誤消息。

如果有人可以看看這個,讓我知道我要去哪裏錯,我會很感激。

非常感謝

SOLUTION

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.";   
} 

回答

0

看起來像所有其他錯誤信息進入一個名爲$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 />";   
} 
+0

嗨,非常感謝您花時間回覆我的帖子。我試着改變消息變量來匹配腳本中的其他消息,但不幸的是我仍然收到不正確的'文件格式'錯誤消息。親切的問候 – IRHM 2011-12-15 18:22:45

+0

如果你使用我的代碼,你仍然收到錯誤信息,那麼我建議回顯你正在計算的文件大小來驗證它是正確的:`echo「FILESIZE:」。($ _ FILES [「fileupload」] [ 「尺寸」]/102400);退出;`另外,如果'文件格式'錯誤信息顯示你應該仔細檢查你的`$ imtype`值 – 2011-12-15 18:31:15