Add contents to ZIP without full path not working - php

I am creating Epub package which I need to pack in the following order
$dest = "ebooks/pack/";
exec("zip -DX0 ".$dest."/book.zip ".$dest."/mimetype ");
exec("zip -DrX9 ".$dest."/book.zip ".$dest."/META-INF ".$dest."/OEBPS");
So I when I pack this , Zip archive structure looks like below
ebooks/pack/mimetype
ebooks/pack/META-INF
ebooks/pack/OEBPS
But I need that files to be in home , For example when I un compress It should look like below
mimetype
META-INF
OEBPS
I also tried to navigate via cd ebooks/pack/ , that throws the error
zip warning: name not matched: mimetype
I even used full path like home/user/public_html/ful
exec("cd home/sam/public_html/domain.com/".$dest."/; zip -DX0 ".$dest."/book.zip mimetype 2&1",$output);
I even tried chdir , It displays the correct path where my files are there
echo getcwd() . "\n";
chdir('home/sam/public_html/domain.com/".$dest."/');
echo getcwd() . "\n";

Related

How to delete laravel delete file after extract?

I am using Zipper to extract uploaded zip file and delete the file after extract.
So I upload and extract like this:
$f = $request['file']->move(public_path($directory), $fullFileName);
\Zipper::make($f)->extractTo(public_path($directory) . $fileName);
and it works great. I've tried to delete the file using these ways.
1 - Storage::disk('products')->delete($fullFileName);
2 - File::delete(public_path($directory) . $fullFileName);
3 - $del = unlink(public_path($directory) . $fullFileName);
but in all actions get resource temporarily unavailable error.
I found this error is because of the zipper (simple files and directories works).
so my question is, How can I delete upload zip file after extract, using a zipper?
Any idea would be great.
thanks in advance.
You need to call $zipper->close(); after you extract it, so if you do something like this it should work:
$zipper = new \Chumper\Zipper\Zipper;
$zipper->make($f)->extractTo(public_path($directory) . $fileName);
$zipper->close();
unlink(public_path($directory) . $fullFileName);
If you do not close the zipper it will not write the result to the disk and keep the original file locked. See the documentation.
$zip = new Zipper;
$zip->make($pathZipFile)->extractTo($destinationPath);
$zip->close(); // important
unlink($pathZipFile); // delete Zip file after

PHP to extract "some-dir/file.ext" from TAR to "another-dir/file.ext"

I use a .tar.gz created monthly by a 3rd party. I can unzip to a .tar; but I am unable to override the tar directory name when I extract files from the tar.
The tar has a single folder "dirname_latestDate" containing a number of files. I was hoping that by specifying individual files for extraction then the tar's "folder" would be ignored (test script cobbled from code on stackoverflow and elsewhere).
$archive = new PharData($theTar);
// error cheecking excluded
foreach($archive as $entry) {
$extractDir = basename($file) . '/';
if($file->isDir()) {
$dir = new PharData($file->getPathname());
foreach($dir as $child) {
$extract_file = $extractDir . basename($child);
$archive->extractTo('/mypath/my-dir', $extract_file, true);
}
}
}
But this still results in the files being placed in a sub-directory named as per tar folder e.g. /mypath/my-dir/unwanted-tar-dirname/file.ext.
I can copy the file from the sub-dir and then delete but this seems ineffficient and unnecessary. Is there any way to override the Tar folder name on un-tar?
The tar contains one file of about 4MB (uncompressed) and a few other miniscule files.
No, you can't change the output file name directly with extractTo.
You could however use file_put_contents in combination with $phar['filepath]->getContent() to extract the file manually.

php script, cannot apply command to a generated file

I have a very simple code, but the final command I can not apply it to the generated file, I can not rename the file and I don't know why. it is not a renaming issue because if I change the renaming for $cmd ="cp '$textfile''$file'" or $cmd ="mv '$textfile''$file'", it does not work either. I better explain it with the code:
<?php
// the original file to work with
$file = "COPYING.odt";
//change the extension of doc|pdf|docx|odt|rtf files to txt
$txtfile = preg_replace('"\.(doc|pdf|docx|odt|rtf)$"', '.txt', $file);
//convert the odt file into txt and name it with the original name but txt extension
$cmd = "/usr/bin/unoconv -f txt '$file' -o '$textfile'";
shell_exec($cmd);
//store the file in the originals files folder
rename("$file", "orig/$file");
//rename the generated txt file with the original name and extension "COPYING.odt"
rename("$textfile", "$file");
echo "conversion of file <em>$file</em> done";
?>
Any suggestions? Thanks.
UPDATE One thing I have forgoten to tell is that I apply this script to an uploaded file, so I can not give the static name of $file, but the variable $file.
UPDATE 2 Following #AlanMachado suggestion I've changed mv command for rename, and this rename works but not the next.
Sorry guys, I've been three days with this script, that's why I ask the question in Stackoverflow, but just now I've realised that it is a TYPO error, yes:
$txtfile = preg_replace('"\.(doc|pdf|docx|odt|rtf)$"', '.txt', $file);
txtfile without e in txt, while
rename("$textfile", "$file");
textfile with e in text, I'm really sorry.
Anyway I don't know why the results of this command was the file with the right name:
$cmd = "/usr/bin/unoconv -f txt '$file' -o '$textfile'";

PHPMailer unable to attach file using relative path

The contact form I'm working on uses an option to select to upload an attachment through file upload or relative file path located on the server. The first option has no issues, but the second won't work and sends without attachment.
I tried multiple combination but nothing seems working. How would I do this?
$mail->AddAttachment("cvs/test-docx_3094.docx", $name = 'test-docx_3094.docx', $encoding = 'base64', $type = 'application/octet-stream' );
$mail->AddAttachment("cvs/test-docx_3094.docx");
$mail->AddAttachment("http://mydomain.com/cvs/test-docx_3094.docx");
any input? Ive been working on this for so long, just cant get it done.
PLMK.
You need to use the absolute filepath.
To show what i mean i assume you have the following directory structure:
app/classes/mailer.php
app/data/test-docx_3094.docx
app/data/test-docx_3095.docx
You could use the following php to get the absolute path in mailer.php:
$relativePath = "cvs/test-docx_3094.docx";
$absolutePath = realpath(dirname(__FILE__) . '/../data/') . $relativePath;
EDIT
Considering your comment you have the following file structure:
website/
contents/
myplugins/
cvs/
test-docx_3094.docx
phpmailer.php
So with this the absolute path would be:
$relativePath = "cvs/test-docx_3094.docx";
$absolutePath = realpath(dirname(__FILE__) . '/cvs/') . $relativePath;
if your php script is located in the myplugins-folder.

PDFLib TET PHP: Can`t extract images

I was able to install TET (php_tet.dll) on Windows 8.1 + Xampp and I have no problems with PDF to Text, but I had no luck with image extraction.
I'm using the example "image resources.php" ( and "image_extractor.php" ) which is supposed to "print" some info about the images (x, y, width, height, alpha and e.t.c) in PDF file. Also must save/extract all available (or any) images into files (tiff, jpg).
The examples can be found here: http://goo.gl/ZeDlc0
The part with image information is working, but there is no files extracted.
I haven't got any trouble with text extraction to TXT file in the same folder.
So I'm able to write there ?
Is something wrong with my SEARCHPATH or else ?
My TRY:
The original example throws ERROR:
Error 1016 in open_document(): Couldn't open PDF file 'FontReporter.pdf' for reading (file not found)
So I changed the SEARCHPATH:
/* global option list */
$globaloptlist = "searchpath={{../data} {../../data} }";
with the location of my pdf file:
/* global option list */
$globaloptlist = "searchpath={{D:\Workshop\www\TET\data} }";
Now I have some output data via print/echo:
page 7: 208x277pt, alpha=0, beta=0 id=0, 595x750 pixel, 1x8 bit Indexed
page 7: 208x277pt, alpha=0, beta=0 id=1, 595x750 pixel, 1x8 bit Indexed
The $tet->write_image_file method returns 10 which says "I can extract TIFF file".
But no images are extracted in my pdf`s folder or anywhere around...
Somehow the images are exported in D:\workshop\xampp\apache
In the option FILENAME I need to set the ABSOLUTE path and the filename...
$path = str_replace('\\', '/', __DIR__);
$imageoptlist = $baseimageoptlist . " filename {".$path."/out/" .
$outfilebase . "_p" . $pageno . "_I" . $ti->imageid . "}";
if ($tet->write_image_file($doc, $ti->imageid, $imageoptlist) == 0){
print("Error " . $tet->get_errnum() . " in " .
$tet->get_apiname() . "(): " . $tet->get_errmsg());
}
this is exactly what I found in the TET manual, (chapter 3.9 "PHP" section):
File name handling in PHP
Unqualified file names (without any path component) and relative file names are
handled differently in Unix and Windows versions of PHP:
- PHP on Unix systems will find files without any path component in the directory
where the script is located.
- PHP on Windows will find files without any path component only in the directory
where the PHP DLL is located.
So I guess, it's expected that you have to adjust the sample slightly for your needs.

Categories