最近更新: 2007-03-19

Read an uploaded spreadsheet document by php-SpreadsheetReader

In PHP, the global array $_FILES will contain all the uploaded file information. However, the temporary filename of the file in which the uploaded file was stored on the server is not the same as source filename. If you pass the temporary filename to SpreadsheetReaderFactory, it could not detect which reader to suit.

The solution is simple. We simply take the extension name from $_FILES['userfile']['name'] and pass it to SpreadsheetReaderFactory. Then we pass $_FILES['userfile']['tmp_name'] to reader. That's all.

The following is a sample. There is a file field named 'SpreadsheetFile' in the form. PHP will put information of this uploaded file in $_FILES['SpreadsheetFile'].

<?php
if (isset($_FILES['SpreadsheetFile'])
    and is_uploaded_file($_FILES['SpreadsheetFile']['tmp_name']))
{
	//print("{$_FILES['SpreadsheetFile']['name']} uploaded.\n");
	$listFilePath = $_FILES['SpreadsheetFile']['tmp_name'];
	$listFileExt = pathinfo($_FILES['SpreadsheetFile']['name'], PATHINFO_EXTENSION);
}

require_once 'SpreadsheetReader/SpreadsheetReaderFactory.php';

$reader = SpreadsheetReaderFactory::reader($listFileExt);
$sheets = $reader->read($listFilePath);
?>
樂多舊網址: http://blog.roodo.com/rocksaying/archives/2878923.html