throw exception when identifying invalid file

Topics: Developer Forum
May 24, 2011 at 2:02 PM


When $inputFileName in PHPExcel_IOFactory::identify($inputFileName) points to an invalid file, e.g. a text file, it should throw an exception. At least that is what the comment of this method suggests:

	 *	Identify file type using automatic PHPExcel_Reader_IReader resolution
	 *	@static
	 *	@access public
	 *	@param 	string 		$pFileName
	 *	@return	string
	 *	@throws	Exception

During unit testing of my code that is using this method I noticed that no exception is raised.

May I suggest to throw an exception? I've added the following three lines in Reader/Excel2007.php, method canRead(). Lines 203-205 are new.

200		if ($zip->open($pFilename) === true) {
201			// check if it is an OOXML archive
202			$rels = simplexml_load_string($this->_getFromZipArchive($zip, "_rels/.rels"));
203			if ($rels === false) {
204			    throw new Exception("File " . $pFilename . " contains no valid data.");
205			}

May 24, 2011 at 10:54 PM

The canRead() method should throw an exception in exceptional circumstances, such as when the file doesn't exist. If the file is not of the appropriate type for the reader, then the canRead() method returns a boolean false. This is the appropriate behaviour for the canRead() method.

Note that the tests in the Excel2007 canRead() method have been changed in the latest code, so $rels is tested, and the actual content is tested to prevent .docx or .pptx files being erroneously identified as workbooks.

I will modify the canRead() code to throw exceptions if any of the relevant libraries (ZipArchive or gzlib) are not enabled, and modify the createReaderForFile() and identify() methods in the IOFactory to prevent aborting the tests if these exceptions are caught so that they will still test for all file types until a match is found.