0

I have to check .xlsx file extension is valid or not. I have one test.docx for the validation purposes I change the file extension test.docx to test.xlsx. So I how can handle this. I am using PHP Excel.

sandip kakade
  • 1,346
  • 5
  • 23
  • 49
  • I think you are looking about this http://stackoverflow.com/questions/13626678/phpexcel-how-to-check-whether-a-xls-file-is-valid-or-not – Rakesh Mishra Feb 15 '17 at 07:59
  • @RakeshMishra I tried this code check only extension not check the data. – sandip kakade Feb 15 '17 at 08:01
  • So I am right that you want to check the content and not the extension? You can't check the content if its "excel" content. You only can check if the data is what you expect. For example, if there is a field with a specific attribute or something. – Twinfriends Feb 15 '17 at 08:16

2 Answers2

1
    $filename = $_FILES['html_form_name']['name'];
    $ext = pathinfo($filename, PATHINFO_EXTENSION);
    if($ext == 'xlsx'){
//it is xslx
}else{
//handle error
}

also

$file_name = "word_document.docx";
        $extension = pathinfo($file_name);
        echo "Your file extension is ".$extension ['extension'];

Also if you are using PHP >5.3.6,

 <?php

    $info = new SplFileInfo('document_name.xlsx');
    var_dump($info->getExtension());
    ?>
Rotimi
  • 4,783
  • 4
  • 18
  • 27
  • Show error `Undefined index: html_form_name` – sandip kakade Feb 15 '17 at 07:55
  • change the 'html_form_name' to the field name from your html form. Whatever attribute you have for 'name' is what you add here. This is just an example as you did not post your html form – Rotimi Feb 15 '17 at 07:57
  • it's validate the extension only not content . – sandip kakade Feb 15 '17 at 08:03
  • to validate the content, you need to open the file and perform your custom checks. Go through the PHPExcel docs. You did not say what you needed. You said you want to check just the file extension – Rotimi Feb 15 '17 at 08:16
1

I have done it like this. It is based on the file name, not on the mime type.

 $extension = end((explode(".", $fileName)));
  • exploding on .
  • getting the last value of array

    if ($extension == "your valid extension"){ //your logic goes here
    
Danyal Sandeelo
  • 12,196
  • 10
  • 47
  • 78