2010-03-01 46 views
0

在我的表單中,我要求有人上傳PDF文件&的圖像。他們都被髮送到相同的陣列$_FILES['rfiles']陣列。我想確認$_FILES['rfiles']['type'][0]爲「application/pdf」或「text/plain」,$_FILES['rfiles']['type'][1]爲「image/*」。第一部分是代碼的工作部分。我試圖最大限度地使用(Foreach/[$key])。我試圖在不使用表格NAME='rfiles[]'的情況下發送2個文件,但是$_FILES有一個限制。因此,無論如何,Foreach方法似乎都必須使用。

foreach ($_FILES['rfiles']['error'] as $key => $error) { 
    $tmp_name = $_FILES['rfiles']['tmp_name'][$key]; 
    if (!$tmp_name) continue; 

    $name = basename($_FILES['rfiles']['name'][$key]); #the recycled variable 

    #This is where the other code would rest. 

    if ($error == UPLOAD_ERR_OK) 
{ 
    if (file_exists("/restdata/" . $_FILES["rfiles"]["name"][$key])) #Checking for a duplicate filename 
     { 
    echo $_FILES["rfiles"]["name"][$key] . " is currenty in use. Please rename your file to a unique name and try again.<br />\n";$badul[$key]=1; #badul[] is to be used later in the page in the followup form. 
     } 
    else 
     { 
      if (move_uploaded_file($tmp_name, "/restdata/".$name)) { 
      $uploaded_array[] .= "Uploaded file '".$name."'.<br/>\n"; 
       echo "File is valid, and was successfully uploaded.\n<br />\n"; 
       echo ($_FILES["rfiles"]["type"][$key]. " is a proper format.<br />\n"); #Mostly being used for testing purposes to get the correct MIME. But, how do I validate it? 
       echo ($uploaded_array[$key] . "<br>\n"); 
       } 
     else 
       { 
     $errormsg .= "Could not move uploaded file '".$tmp_name."' to '".$name."'<br/>\n"; 
       } # end if/else move_upoaded File 
      } #end if/else file_exists 
} #end if $error == UPLOAD_ERR_OK 
else $errormsg .= "Upload error. [".$error."] on file '".$name."'<br/>\n"; 
    echo ($errormsg); 
} #End Foreach 

我試圖保持foreach循環完好嘗試2種不同的方法,第一個將是最優的,因爲我將是安倍使用$鍵保持,但你似乎無法挺過這一關數組中的數組。我刪除這些從我工作的腳本,因爲這些錯誤似乎unpassable

#absolutey predefine these variables before the Foreach statement. 
$allowedExtensions[0] = array("txt", "rtf", "doc", "pdf"); 
$allowedExtensions[1] = array("gif", "jpg", "jpeg"); 

     ... 
     $file = $_FILES['rfiles']; 

     function isAllowedExtension($fileName) { 
      global $allowedExtensions[$key]; #tricky tricky? 

      return in_array(end(explode(".", $fileName)), $allowedExtensions[$key]); 
     } 

     if($error == UPLOAD_ERR_OK) { 
      if(isAllowedExtension($file['name'][$key])) 
     ... 

另一種方法只留下太多窟窿,因爲如果參數1通過,也不會不管是什麼第二個文件是:

... 
if (((($_FILES["rfiles"]["type"][1] == "image/gif") 
    || ($_FILES["rfiles"]["type"][1] == "image/jpeg") 
    || ($_FILES["rfiles"]["type"][1] == "image/pjpeg")) 
    && ($_FILES["rfiles"]["size"][1] < 5000000)) 
    || ((($_FILES["rfiles"]["type"][0] == "text/plain") #probaby the wrong syntax, but worth a shot. 
    || ($_FILES["rfiles"]["type"][0] == "application/pdf") 
    || ($_FILES["rfiles"]["type"][0] == "text/rtf")) 
    && ($_FILES["rfiles"]["size"][0] < 15000000))) 
     { 
     if ($error == UPLOAD_ERR_OK)... 

我試圖通過$_FILES &直接攻擊它試圖爆炸它..有沒有另一種方式?

編輯:爲了說明的目的 - 我需要能夠分別處理文件驗證。他們的價值觀被單獨處理成一個數據庫。無法爲客戶提供特定的錯誤會在他們的最終造成太多混亂。

回答

0

用以下內容替換你的第二個方法,那麼它應該按預期工作:

... 
if (((($_FILES["rfiles"]["type"][1] == "image/gif") 
    || ($_FILES["rfiles"]["type"][1] == "image/jpeg") 
    || ($_FILES["rfiles"]["type"][1] == "image/pjpeg")) 
    && ($_FILES["rfiles"]["size"][1] < 5000000)) 
    && ((($_FILES["rfiles"]["type"][0] == "text/plain") #probaby the wrong syntax, but worth a shot. 
    || ($_FILES["rfiles"]["type"][0] == "application/pdf") 
    || ($_FILES["rfiles"]["type"][0] == "text/rtf")) 
    && ($_FILES["rfiles"]["size"][0] < 15000000))) 
    { 
    if ($error == UPLOAD_ERR_OK)... 
+0

這解決了一般驗證。 不幸的是,留下一個全或無的情況下,那麼我將無法單獨處理文件進行驗證。因此,離開「||」的初步想法聲明。 再次回到「Foreach」的想法。 – Kamikaze478 2010-03-02 04:22:01

相關問題