if else statemetn检查上传的文件类型

时间:2016-07-07 14:24:08

标签: php

我必须检查上传的文件是否为pdf类型..如果是pdf则执行操作或其他操作

 <?php
   $text = $fileurl[0]['filename'];
    $string = '.pdf';
    if( strpos($string, $text) !== false ){?>
    <div id="example1"></div>
    <script src="http://localhost/sookoon1/pdf/pdfobject.js">  </script>
     <script>PDFObject.embed("http://localhost/sookoon1/system/storage/download/<?php  echo  $fileurl[0]['filename'];?>", "#example1");</script>
  <?php var_dump($fileurl); ?>

     <style>
      .pdfobject-container { height: 1000px;}
     .pdfobject { border: 1px solid #666; }
      </style>
    <?php } ?>
    else {?>
     <?php var_dump($fileurl); ?>
     <a href="http://localhost/sookoon1/epub/bib/i/?book=<?php echo    $fileurl[0]['filename'];?>" data-bibi="embed" data-bibi-style="[[ CSS for embeded BiB/i frame, as you like ]]">My Great Book Title</a><script src="http://localhost/sookoon1/epub/bib/i.js"></script>
<?php }?>    }

1 个答案:

答案 0 :(得分:0)

您可以/应该使用finfo检查文件内容

/**
 * DO NOT TRUST $_FILES[0]['mime'] VALUE !!
 * Check MIME Type by yourself.
 */
$finfo = new finfo(FILEINFO_MIME_TYPE);
if (false === $ext = array_search( $finfo->file($fileurl[0]['filename']]),
    array('pdf' => 'application/pdf' ), true )) {
    $msg='Invalid file format.';
    echo $msg;
    /**
     * Here goes the Aktion when the file IS NOT not a pdf document !
     */
}else{
    /**
     * Here goes the Aktion when the file IS a pdf document !
     *
     * You can check filesize for example.
     */


}