2011-12-14 112 views
0

我想知道是否有人可以幫助我。設置文件大小限制信息

我不得不承認,我相對較新的PHP編寫,請耐心等待。

通過我在互聯網上閱讀的文章和一些@Marcio在這個網站上的一流教程,我編寫了一個允許用戶將圖像文件保存到mySQL數據庫的腳本。

我已經通過限制可以上傳的文件的大小進一步了一點,但我想添加一條警告消息,告訴爲什麼文件無法上傳,即因爲它的大小大於限制設置。

我已經對此做了一個嘗試,如下面的代碼所示。但不幸的是,我收到一條錯誤消息,指出有一個意外的'>',我知道它與我添加的行有關,但不知道如何以另一種方式編碼。

修訂砍下代碼

<?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'; 
      } 
     } 

我只是想知道是否有人也許可以看看這個,讓我知道我做錯了什麼。

非常感謝和親切的問候像素

+0

哪條線有故障?並且不要使用@來抑制調試錯誤。 – ajreal 2011-12-14 16:07:40

回答

0

您可以使用此

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 />"; 
    } 
+0

嗨,非常感謝您查看和回覆我的帖子。我試過了你所建議的代碼,但是,這很可能是因爲我缺乏經驗。即使文件大小超過限制,而不是接收到適當的消息,我仍然收到'未知格式'消息。我已經修改了我的原始帖子以顯示新內容。我只是想知道你是否可以看看這個請告訴我哪裏出錯了。再次非常感謝和親切的問候。 – IRHM 2011-12-14 17:58:53

0

getfilesize()返回圖像的尺寸,而不是文件大小。你需要沿着這個線的東西:

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