PHP Example
Caution
For server security, please refer to the link below when handling this work.
[Software Security Weakness Diagnosis Guide]
1. Prerequisites
- PHP
ZipArchive(http://php.net/manual/en/class.ziparchive.php)
2. Image Upload (Video and File uploads use the same pattern)
Caution
The file-upload portion of the sample code below is intentionally minimal and lacks proper security handling.
For the file-upload portion, use what is already in place inside your project, and refer to the code below for the integration portion only.
<?php
try {
$uploadDir = 'uploads/images';
$fieldName = 'file';
$fileNameParts = explode('.', $_FILES[$fieldName]['name']);
$extension = end($fileNameParts);
$tmpName = $_FILES[$fieldName]['tmp_name'];
$newFileName = sha1(microtime());
$fileUploadPath = "${uploadDir}/${newFileName}.${extension}";
move_uploaded_file($tmpName, $fileUploadPath);
header('Content-Type: application/json');
echo json_encode(['uploadPath' => $fileUploadPath]);
} catch (Exception $e) {
echo $e->getMessage();
http_response_code(404);
}
3. HWP / Word / Excel Document Import
Caution
The file-upload portion of the sample code below is intentionally minimal and lacks proper security handling.
For the file-upload portion, use what is already in place inside your project, and refer to the code below for the integration portion only.
<?php
try {
$uploadDir = 'uploads/docs';
$fieldName = 'file';
$fileNameParts = explode('.', $_FILES[$fieldName]['name']);
$extension = end($fileNameParts);
$tmpName = $_FILES[$fieldName]['tmp_name'];
$newFileName = sha1(microtime());
$fileUploadPath = "${uploadDir}/${newFileName}.${extension}";
move_uploaded_file($tmpName, $fileUploadPath);
// Conversion output directory
$wordDir = 'works';
$importPath = "${wordDir}/${newFileName}";
executeConverter($fileUploadPath, $importPath);
// Serialize document.pb
// From v2.3.0 the filename is "document.pb" (was "document.word.pb")
$pbFilePath = "${importPath}/document.pb";
$serializedData = readPBData($pbFilePath);
header('Content-Type: application/json');
echo json_encode([
'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: use gzuncompress instead
// $byteArray = unpack('C*', gzuncompress($data));
return array_values($byteArray);
}