2011-09-03 67 views
0

我目前在上传脚本时遇到了一些问题。 它不检查文件类型是否允许,我不明白为什么。 有没有人看到我的问题?PHP:图像检查扩展

   //Reads the image name 
      $image=$_FILES['image']['name']; 

      //If empty 
      if ($image == "") { 
      echo '<div class="box_square form_error" style="margin-bottom:15px; padding:10px; font-size:11px;">Velg et bilde!</div>'; 
      $errors = 1; 
      } 

      //If not empty 
      if ($image) 
      { 
       //Get orgiginal name from client machine 
       $filename = stripslashes($_FILES['image']['name']); 
       //Get the extension 
       $extension = getExtension($filename); 
       $extension = strtolower($extension); 

       //If file extension not known, error. If not, continue 
       if (($extension != "jpg") && ($extension != "jpeg")) 
       { 
        //Error message 
        echo '<div class="box_square form_error" style="margin-bottom:15px; padding:10px; font-size:11px;">Bare JPG og JPEG er tilatte filtyper!</div>'; 
        $errors = 1; 

       } 
+0

发生了什么,你不想发生? –

+0

当文件类型未知时,它不会回显消息。 – Kaizokupuffball

+0

那么,'$ extension'包含了什么? –

回答

0

它看起来像一个不正确的getExtension($文件名)。但是,我不知道你为什么使用反斜杠。

如何

echo "the extension used is ".$extension."<br>"; 

$extension = strtolower($extension); 

顺便说一句,您使用表达式规则

preg_match("/\.([^\.]+)$/", $filename, $extension); 
+0

那也没有输出任何东西。 – Kaizokupuffball

0

不靠延长,但是,mime type,而不是可以提取扩展。

+1

....如果有人将'hack.php'上传为'image/jpeg',则发布日期? – Jonathon

+0

杰尼杰尼!必须检查他们两个。 – Charlie

+0

你最好在服务器上检查MIME类型,不要相信浏览器发送给你的东西。但是,检查文件扩展名不会有帮助。您的服务器可能会以某种方式解析许多文件扩展名(例如,php,phtml,php3,php4,php5,py,...)。 – str