2012-02-08 78 views
-1

我有一個應用程序(http://helios.hud.ac.uk/u0867587/Files/Files.php)是檢查服務器端的文件?

如果你打開應用程序,點擊「添加問題」按鈕。這將添加一個表格行。現在在文件輸入中選擇一個無效的文件(例如.txt文件)。現在點擊「提交詳細信息」按鈕,它將在問題1中提示「不支持的文件類型」。現在返回文件輸入並將文件更改爲有效格式(.png,.jpg,.jpeg或.gif),然後在完成後單擊「提交詳細信息」按鈕,此時會出現一個確認框,指出一條消息並如果你想繼續。

現在,這一切工作在客戶端的罰款,但我想要的是,當用戶點擊「提交詳細信息」按鈕,我希望它檢查文件格式是在服務器端正確以及爲客戶端安全目的。

什麼我不知道的是,如果下面的代碼是檢查在服務器端的文件,以及在客戶端,因爲沒有信息是來自一個回波出現,如果提交的右上角後,選擇了錯誤的文件。

下面是PHP代碼:

if(isset($_FILES["imageFile"])) 
{ 

$allowedImageTypes = array("image/pjpeg","image/jpeg","image/jpg","image/png","image/x-png","image/gif"); 
$file = $_FILES['imageFile']; 
$fileType = $file['type']; 
if (!in_array($fileType, $allowedImageTypes)) { 
    echo "Unsupported file type"; 
} 
else 
{ 
    // Process the file 
} 
} 
?> 

下面是對文件的輸入問題編號是如何在每行中添加代碼:

var qnum = 1; 

function insertQuestion(form) { 


    var $tbody = $('#qandatbl > tbody'); 
    var $tr = $("<tr class='optionAndAnswer' align='center'></tr>"); 
    var $qid = $("<td class='qid'>" + qnum + "</td>"); 
    var $image = $("<td class='image'></td>"); 


      var $imagefile = $('<input />') 
     .attr({ 
      type: 'file', 
      name: 'imageFile', 
      class: 'imageFile' 
     }); 

     $image.append($imagefile); 

      var $imageclear = $('<input />') 
     .attr({ 
      type: 'button', 
      name: 'imageClear', 
      class: 'imageClear', 
      value: 'Clear File' 
     }); 

     $image.append($imageclear); 


    $tr.append($qid); 
    $tr.append($image); 
    $tbody.append($tr); 

} 

下面是在錶行表加入:

<form id="enter" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" onsubmit="return validateForm(this);" > 


<div id="details"> 
<table id="qandatbl" align="center"> 
<thead> 
<tr> 
    <th class="qid">Question No</th> 
    <th class="image">Image</th> 
</tr> 
</thead> 
<tbody></tbody> 
</table> 
</div> 

</form> 

回答

1

表格類型應該是

在客戶端計算機上的文件的
<form enctype="multipart/form-data" action="uploader.php" method="POST"> 
<input type="file" name="userfile" /> 

原來的名稱。該文件的

$_FILES['userfile']['name'] 

mime類型,

$_FILES['userfile']['type'] 

大小,以字節爲單位,上傳文件的。

$_FILES['userfile']['size'] 

上載文件存儲在服務器上的臨時文件名。

$_FILES['userfile']['tmp_name'] 

所以

$fileType = $_FILES['userfile']['type']; 

if (!in_array($fileType, $allowedImageTypes)) { 
    echo "Unsupported file type"; 
} 
else 
{ 
    // Process the file 
} 
+0

嗨,你知道的形式採取行動,它必須uploader.php?我想在同一個頁面上顯示的消息,也是在我的編輯我包含在數據如何在表中添加的代碼。如果你寫一個單獨的頁面會更好,因爲它會阻止 – user1182476 2012-02-08 12:54:44

+0

,刷新問題。 – 2012-02-08 13:09:38

+0

那麼,有沒有一種方法,我可以寫一個單獨的頁面上的PHP,但不導航到該頁面呢?我不希望用戶看到uploader.php文件,但我喜歡服務器檢查背景以查看它是否是正確的文件。如果文件類型正確與否,客戶端警報會提醒用戶。我只想在服務器端進行檢查以確保安全 – user1182476 2012-02-08 13:24:51