<?php
// Upload directory
$uploadDir = 'uploads/docs';
// Name of the form data
$fieldName = 'file';
// File name
$fileName = explode('.', $_FILES[$fieldName]['name']);
//Filename extension
$extension = end($fileName);
// Temporary file name
$tmpName = $_FILES[$fieldName]['tmp_name'];
// File name for the new save
$newFileName = sha1(microtime());
// Actual file upload path
$fileUploadPath = "${uploadDir}/${newFileName}.${extension}";
// Save the file
move_uploaded_file($tmpName, $fileUploadPath);
// Directory name to save conversion result
$wordDir = 'works';
// Execute conversion
$importPath = "${wordDir}/${newFileName}";
executeConverter($fileUploadPath, $importPath);
// Serialize document data
// Since v2.3.0, the file name is changed from document.word.pb to document.pb
$pbFilePath = "${importPath}/document.pb";
$serializedData = readPBData($pbFilePath);
include("./JSON.phps");
// Send the response to the client.
header('Content-Type: application/json');
echo json_encode_new(array(
'serializedData' => $serializedData,
'importPath' => $importPath,
));
function executeConverter($inputFilePath, $outputFilePath)
{
$sedocConverterPath = 'c:/sedocConverter/sedocConverter.exe';
$fontsDir = 'c:/sedocConverter/fonts';
$tempDir = 'c:/sedocConverter/tmp';
$cmd = "${sedocConverterPath} -f ${fontsDir} ${inputFilePath} ${outputFilePath} ${tempDir}";
exec($cmd);
}
function readPBData($pbFilePath)
{
$filesize = filesize($pbFilePath);
$zd = gzopen($pbFilePath, "r");
$data = substr( gzread($zd, $filesize), 16 );
gzclose($zd);
$byteArray = unpack('C*', gzuncompress($data));
$serializedData = array_values($byteArray);
return $serializedData;
}
function json_encode_new($data) {
$json = new Service_JSON();
return($json->encode($data));
}
?> |