<?php
try {
// 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);
// Send the response to the client.
header('Content-Type: application/json');
echo json_encode(array(
'serializedData' => $serializedData,
'importPath' => $importPath,
));
} catch (Exception $e) {
echo $e->getMessage();
http_response_code(404);
}
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)
{
$fb = fopen($pbFilePath, 'r');
$data = stream_get_contents($fb, -1, 16);
fclose($fb);
$byteArray = unpack('C*', zlib_decode($data));
// php 5.4.0 or below
// $byteArray = unpack('C*', gzuncompress($data));
$serializedData = array_values($byteArray);
return $serializedData;
}
|