Commit 112c038a authored by Adam Schubert's avatar Adam Schubert

first commit

parents
office2txt
==========
office2txt is PHP file conveter using unocovn
<?php
/**
* Class for converting office files to other format (txt by default) in PHP using unoconv
* @author Adam Schubert <adam.schubert@winternet.cz>
*/
class office2txt
{
/**
* Absolute path to directory where files for conversion are located.
*/
private $absolute_path = '/root';
/**
* Command to run
*/
private $binary = 'unoconv';
/**
* Filename of file to convert (relative to $absolute_path)
*/
private $from = '';
/**
* Constructor, checks file paths
* @param string $from Filename of file to convert (relative to $absolute_path)
* @throws Exception
*/
public function __construct($from)
{
if(!is_file($this->absolute_path.'/'.$from))
{
throw new Exception('File '.$this->absolute_path.'/'.$from.' not found!');
}
else
{
$this->from = $from;
}
}
/**
* Converts file by calling specified command
* @param string $format specifies format to convert, see http://dag.wieers.com/home-made/unoconv/
* @returns string Path to converted file
* @throws Exception
*/
private function convert($format)
{
$pathinfo = pathinfo($this->absolute_path.'/'.$this->from);
$new_name = $this->absolute_path.'/'.$pathinfo['filename'].'.'.$format;
if(is_file($new_name))
{
throw new Exception('File '.$new_name.' exist on file system!');
}
$status = 0;
@system('unoconv --format='.$format.' '.$this->absolute_path.'/'.$this->from,$status);
if($status != 0)
{
throw new Exception('Command failed! Check your '.$this->binary.' instalation!');
}
return $new_name;
}
/**
* Returns converted file as string
* @param $format specifies format to convert, see http://dag.wieers.com/home-made/unoconv/
* @returns string file content
*/
public function getString($format = 'txt')
{
$path = $this->convert($format);
$content = file_get_contents($path);
unlink($path);
return $content;
}
/**
* Returns converted file path
* @param $format specifies format to convert, see http://dag.wieers.com/home-made/unoconv/
* @returns string pat to file
*/
public function getFile($format = 'txt')
{
return $this->convert($format);
}
}
?>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment