Delete system tmp file with COM and Word - php

I am using COM to convert dynamically created word docx to PDFs. Everything is working perfectly, but our c:\windows\temp\ directory is getting extremely large (and we have limited disk space on our C drive). Below is the code I am using to generate the PDF. Is there a way to delete the temp file that gets added to the system's temp directory after the file is closed? In addition, after the word doc is converted to pdf, we no longer need it (so it doesn't matter if deleting the tmp file corrupts the word doc).
/Word Doc to PDF using Com
ini_set("com.allow_dcom","true");
try{
$word = new com('word.application');
}
catch (com_exception $e)
{
$nl = "<br />";
echo $e->getMessage() . $nl;
echo $e->getCode() . $nl;
echo $e->getTraceAsString();
echo $e->getFile() . " LINE: " . $e->getLine();
$word->Quit();
$word = null;
die;
}
$word->Visible = 0; //don't open word gui on server
$word->DisplayAlerts = 0; //don't allow any alerts to open
//open doc
try{
$doc = $word->Documents->Open(DOC_LOCATION . "temp_doc/" . $filename . ".docx");
}
catch (com_exception $e)
{
$nl = "<br />";
echo $e->getMessage() . $nl;
echo $e->getCode() . $nl;
echo $e->getFile() . " LINE: " . $e->getLine();
$word->Quit();
$word = null;
die;
}
try{
$doc->ExportAsFixedFormat(DOC_LOCATION . "temp_pdf/" . $filename . ".pdf", 17, false, 0, 0, 0, 0, 7, true, true, 2, true, true, false);
}
catch (com_exception $e)
{
$nl = "<br />";
echo $e->getMessage() . $nl;
echo $e->getCode() . $nl;
echo $e->getTraceAsString();
echo $e->getFile() . " LINE: " . $e->getLine();
$word->Quit();
$word = null;
die;
}
$word->Quit();
$word = null;

Don't say what version, but for XP- you can use a simple batch file to delete the files in \Documents and Settings\USER\Local Settings\temp\. Example:
C:
cd \Documents and Settings\USER\Local Settings\Temp
attrib -R -S -H *.* /S /D
DEL /S /F /Q *.*
Modify accordingly.

Related

SFTP disconnect() method not working with Laravel

I'm running the following script inside a Laravel job (OperatorSubmissionJob) to upload multiple files to the SFTP server. Everything working well. But I have to disconnect the SFTP connection to end the session after the uploading finished.
$count=1;
$sftp = Storage::disk('sftp');
foreach ($request['image'] as $key => $image) {
$image_array_1 = explode(";", $image);
$image_array_2 = explode(",", $image_array_1[1]);
$data = base64_decode($image_array_2[1]);
$imageName = Carbon::now()->format('Ymd_his') . $key . '.jpeg';
// FTP server upload
$ftp_image_name = $ftp_path . "_" . $count . '.jpeg';
try {
$isUploaded = $sftp->put("$ftp_path/" . $ftp_image_name, $data, 'public');
if ($isUploaded) {
Log::info("FTP image uploaded (UserID-" . $id . ", CustomerID-" . $mail_history->CustomerID . "): " . $count);
} else {
Log::info("FTP image upload failed (UserID-" . $id . ", CustomerID-" . $mail_history->CustomerID . "): " . $count);
}
$count++;
} catch (\Exception $e) {
Log::error($e->getMessage());
}
}
// have to disconnect $sftp here. I have tried the following:
// $sftp->disconnect(); // error: undefined method disconnect
// $sftp->getDriver()->getAdapter()->disconnect(); // error: undefined method disconnect
// $sftp->getFilesystem()->getAdapter()->getClient()->disconnect();
//
Used techs:
Laravel 9 league/flysystem-sftp-v3
None of the disconnect scripts working in my case. Please help..
I have tried with the following scripts:
$sftp->disconnect();
$sftp->getDriver()->getAdapter()->disconnect();
$sftp->getFilesystem()->getAdapter()->getClient()->disconnect();

How do I create a new log file if my current log file is larger than 50mb in php?

I have a project where I have to make a new log file if the current log file is larger than 50mb. I know that we have to test for the size of the file.
I am starting out with this $log variable:
$log =
"Branch: ".$branchDir . PHP_EOL.
"Phase: ". $phaseDir . PHP_EOL.
"Total number of results files deleted: ". count($folderCounter). PHP_EOL.
"File names: " . $filePathToDelete . PHP_EOL.
"Starting CustomerID directory name: " . $CustIDvalue . PHP_EOL;
// $log = 'this is a test' . $branch . PHP_EOL;
$dt = time();
$mysql_datetime = strftime("%Y-%m-%d %H:%M:%S", $dt);
$mysql_datetimes = preg_replace('/\s+/', '', $mysql_datetime);
$logFileName = "ResultsFileDeletion" . $mysql_datetimes . ".txt";
if (unlink($filePathToDelete)) {
//this echo string will be moved to a log file
echo "first unlink: {$filePathToDelete} was deleted <br>";
$myFile =
file_put_contents("c:\\sites\\EtonBio/sequencing/logs/{$logFileName}",
$log, FILE_APPEND);
echo 'my file: ' . $myFile . '<br>';
}
I found this code snippet:
$size = filesize($_FILES['foto']['tmp_name']);
I believe my if statement would look something like this:
if($size < 52428800) {
*creates another log file
}
I want to format the $size variable correctly and I am also not sure how to make a new log file.
$size = filesize($_FILES['foto']['tmp_name']); only works if you are uploading the file.
If you want to check a file already in you server ou can simply do
$size = filesize("path/to/file");
Also, there is a ' in your $log declaration that shouldn't be there

Laravel mysqldump create empty file, but same command work when execute via xampp shell

running Laravel 5.2 on Xampp 321 Win7 Mysql5.6 PHP 5.6.3 I cant create BD dump file .sql
$filename = "backup-".Carbon\Carbon::now()->format('Y-m-d_H-i-s').".sql 2>&1";
try{
$command = "mysqldump --user=" . env('DB_USERNAME') ." --password=" . env('DB_PASSWORD') . " --host=" . env('DB_HOST') . " " . env('DB_DATABASE') . " > " . storage_path() . "/" . $filename;
$returnVar = NULL;
$output = NULL;
//exec command allows you to run terminal commands from php
exec($command, $output, $returnVar);
//dd($command);
return 1;
}catch(Exception $e){
return $e->errorInfo; //some error
}
When loading script it generates an empty file! But by doing dd ($ command) and copying paste this text, this command works fine in the Xampp shell. Any ideas please?
Solved with this code: setting absolute path of mysqldump adn adding double backslash to url var
$filename = "backup-".date("d-m-Y-H-i-s").".sql";
$mysqlPath = "D:\\xampp/mysql/bin/mysqldump";
try{
$command = "$mysqlPath --user=" . env('DB_USERNAME') ." --password=" . env('DB_PASSWORD') . " --host=" . env('DB_HOST') . " " . env('DB_DATABASE') . " > " . storage_path() . "/" . $filename." 2>&1";
$returnVar = NULL;
$output = NULL;
exec($command, $output, $returnVar);
return 1;//ok
}catch(Exception $e){
return "0 ".$e->errorInfo; //some error
}

Word Doc File to PDF on Windows Server

I am trying to convert .docx files to pdf using PHP on a Windows server. I tried several of the solutions from other posts, including phpdocx (which does a very poor conversion that doesn't keep any formatting) and php's Com object. I only have Office 2003, so there is no pdf converter available using Com.
I thought of using OpenOffice/LibreOffice, but haven't found any information about installing and using Com for these on a windows server (I know it can be installed, but I can't figure out how to set up Com for it).
Using a webservice is not an option due to the data on the forms (they have to remain on our server). This means that Zend Framework cannot be used.
Any suggestions would be helpful, or information about using Com with Open Office.
I was finally able to get this working. OUr problem was that Word 2003 didn't have a PDF converter in it. We ended up using a trial version of Office 2010 for now (assuming that everything works correctly, we will purchase the full version). Word 2007 would have worked also. Below is the code I used to get this working:
//Word Doc to PDF using Com
ini_set("com.allow_dcom","true");
try{
$word = new com('word.application') or die('MS Word could not be loaded');
}
catch (com_exception $e)
{
$nl = "<br />";
echo $e->getMessage() . $nl;
echo $e->getCode() . $nl;
echo $e->getTraceAsString();
echo $e->getFile() . " LINE: " . $e->getLine();
$word->Quit();
$word = null;
die;
}
$word->Visible = 0;
$word->DisplayAlerts = 0;
try{
$doc = $word->Documents->Open(DOC_LOCATION. 'test_image.docx');
}
catch (com_exception $e)
{
$nl = "<br />";
echo $e->getMessage() . $nl;
echo $e->getCode() . $nl;
echo $e->getFile() . " LINE: " . $e->getLine();
$word->Quit();
$word = null;
die;
}
echo "doc opened";
try{
$doc->ExportAsFixedFormat(DOC_LOCATION . "test_image.pdf", 17, false, 0, 0, 0, 0, 7, true, true, 2, true, true, false);
}
catch (com_exception $e)
{
$nl = "<br />";
echo $e->getMessage() . $nl;
echo $e->getCode() . $nl;
echo $e->getTraceAsString();
echo $e->getFile() . " LINE: " . $e->getLine();
$word->Quit();
$word = null;
die;
}
echo "created pdf";
$word->Quit();
$word = null;

PHP PDFlib unicode

is it possible to render pdf in UTF-8. for persian or arabic characters? i dont wanna use tcpdf. it's have a performance problem during make pdf via php scripts. I will try latest version of php 5.4 and pdflib 8.0.4p2 but i cant see valid persian words.
<?php
try {
$p = new PDFlib();
/* open new PDF file; insert a file name to create the PDF on disk */
if ($p->begin_document("", "") == 0) {
die("Error: " . $p->get_errmsg());
}
$p->set_info("Creator", "hello.php");
$p->set_info("Author", "Rainer Schaaf");
$p->set_info("Title", "Hello world (PHP)!");
$p->begin_page_ext(595, 842, "");
$font = $p->load_font("Courier", "unicode", "");
$p->setfont($font, 24.0);
$p->set_text_pos(50, 700);
$p->show("Hello world!");
$p->continue_text($p->utf8_to_utf16("(says PHP) سلام جهان", 'utf16'));
$p->end_page_ext("");
$p->end_document("");
$buf = $p->get_buffer();
$len = strlen($buf);
header("Content-type: application/pdf");
header("Content-Length: $len");
header("Content-Disposition: inline; filename=hello.pdf");
print $buf;
}
catch (PDFlibException $e) {
die("PDFlib exception occurred in hello sample:\n" .
"[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " .
$e->get_errmsg() . "\n");
}
catch (Exception $e) {
die($e);
}
$p = 0;
The main problem is that the font Courier does not have glyphs for the arabic text you want to print. If you use a font like Arial Unicode you will see the expected result.
Another tip, use $p->set_parameter("textformat", "utf8");
then you can use utf8 directly as in $p->continue_text("(says PHP) سلام جهان x");

Categories