2012-02-08 77 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