简体   繁体   中英

Detect an edited csv file using PHPExcel

I am using PHPExcel to validate csv files before parsing them and storing in my database and server. I am trying to use the file properties to determine if the file has been modified or if it is the original file. I have used the following for .xls, .xlsx with great results (using the appropriate reader);

    $file = $_FILES['file']['tmp_name'];
    $reader = new PHPExcel_Reader_CSV();
    if($reader->canRead($file)){
        $object = $reader->load($file);
        $created = $object->getProperties()->getCreated();
        $modified = $object->getProperties()->getModified();
        if(!$created===$modified){
             //File has been edited and cannot be used
        }else{
            //File is good, continue processing
        }
    }

However, when using CSV files, NOTHING is working as expected. I renamed an MS-Word doc to .csv->passed, edited a csv->passed, even used a .jpg->passed. What on earth am I missing?? Any help would greatly appreciated! Edit->I should note that $created and $modifed are an exact match when var_dump($object) despite having edited the file and confirming the changes within the document properties.

The properties values accessible from PHPExcel are those stored within the file itself, not within the directory entries for that file.

CSV files don't have any inherent properties of their own; CSV is purely a raw data file format These property methods are for accessing the properties that do exist in other spreadsheet formats such as BIFF (xls) and OfficeOpenXML (xlsx) which do support them. Loading a CSV (or other format that doesn't support properties) into PHPExcel will provide default value for those properties (so that calls like you're making won't trigger fatal errors), but it cannot provide actual values for something that doesn't natively exist in the format being loaded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM