I am creating something like easy installer\setupper for my CMS. When user downloads .zip with my CMS and unpacks it into some folder on his php server he sees a file - install.php and a zip folder named - Contents.zip I need some php function to extract files from that Contents.zip zip file and than delete that file. (If it is possible I want to give different rights to files\folders extracted from there right after folder unZipping)
How to do such a thing?
you could use PHP ZipArchive library to for you purpose.
also, an code example from the documentation you might find useful.
<?php
$zip = new ZipArchive();
$filename = "./test112.zip";
if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
exit("cannot open <$filename>\n");
}
$zip->addFromString("testfilephp.txt" . time(), "#1 This is a test string added as testfilephp.txt.\n");
$zip->addFromString("testfilephp2.txt" . time(), "#2 This is a test string added as testfilephp2.txt.\n");
$zip->addFile($thisdir . "/too.php","/testfromfile.php");
echo "numfiles: " . $zip->numFiles . "\n";
echo "status:" . $zip->status . "\n";
$zip->close();
?>
for changing the file permissions you can use PHP builtin function chmod.
for example
chmod("/somedir/somefile", 0600);
to delete the file you can use, php builtin unlink
for example,
unlink('test.html');
unlink('testdir');
I would strongly recommend yuo to go through the official PHP documentation.
Related
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
I am using a Zipper package for making zip files out of API fetched PDF's. Zipping works fine but I would like to delete PDF files that were zipped.
$pdf_summary_filename = public_path() . $path . uniqid() . '_summary.pdf';
PDF::loadView('pdf.summary', $pdf_data)->save($pdf_summary_filename);
$zipper->make($zip_filename)->add($pdf_summary_filename);
File::cleanDirectory(public_path() . '/user_downloads');
I am using this code, however, I think that cleanDirectory() gets called before the zipping finishes, and I see no zip generated. If I comment out the last line, I get both the zip file as well as PDF's in /user_downloads.
How can I wait for the zipper to finish zipping?
UPDATE: You can try below code:
$flgFile = $zipper->make($zip_filename)->add($pdf_summary_filename);
if($flgFile){
File::cleanDirectory(public_path() . '/user_downloads');
}
This may help you better!
I'm creating a PHP script, which supposed to extract a zip archive stored on the php file directory to a folder.
Everything works well, but when I check te result, I find 2 folders under the directory: a folder with the name of the zip archive, and another folder named __MACOSX. I don't know how this folder came there, especially as I'm using Windows 7. Second, in each folder there is a file called .DS_Store.
Now, I don't know how these things got there. This is my code:
$zip = new ZipArchive;
if ($zip->open('File.zip')) {
$path = getcwd() . "/details/" . trim($id) . "/";
$path = str_replace("\\","/",$path);
echo $path;
echo $zip->extractTo($path);
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
This is the only code that extracts the zip file, or touching it, and as you can see, there is nothing like __MACOSX or .DS_Store.
Can you please help me?
File.zip originated on a OSX system. __MACOSX and .DS_Store have 0 usage or bearing on any other OS. Delete / Ignore them and keep trucking.
As an aside, you may want to add the stated file system objects to your project .gitignore.
https://superuser.com/questions/104500/what-is-macosx-folder
https://en.wikipedia.org/wiki/.DS_Store
i created a text a file in the current directory using getcwd(), now i need create the text in a different directory. but i cant figure out how to go back using only php
what it did before
when im at
/var/www/website/mydomain/
the code is
$objData = serialize($name). "\r\n";
$createPath = getcwd().DIRECTORY_SEPARATOR."mytextdir/".$id.".txt";
echo $createPath;
$fp = fopen($createPath, "w");
fwrite($fp, unserialize($objData));
fclose($fp);
it creates the text in
/var/www/website/mydomain/mytextdir/###.txt
but now i need to create this text file on
/var/www/website/allTextfiles/###.txt
is this possible if so, can anybody help me on how to do it using something similer to this technique
getcwd().DIRECTORY_SEPARATOR."#####/".$##.".txt";
With double-dot you get to a higher folder.
So your path changes into this:
getcwd() . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . "allTextFiles/".$id.".txt";
I've been at this for a while. This actually worked one time, then never again. it simply does not create the zip file. The file does exist.
$zip = new ZipArchive();
$filename = "./test" . time() .".zip";
if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
exit("cannot open <$filename>\n");
}
$thisdir = "$_SERVER[DOCUMENT_ROOT]/zip";
$zip->addFile($thisdir . "/trash-icon.png", "/gabage.png");
echo "numfiles: " . $zip->numFiles . "\n";
echo "status:" . $zip->status . "\n";
$zip->close();
If I add something like
$zip->addFromString("testfilephp.txt", "#1 This is a test string added as testfilephp.txt.\n");
it creates the zip with the txt file in it.. but a no go for anytype of existing file.
The ZipArchive::addFile() method accepts the path to the file as its first parameter, but not all paths are created equal. addFile() method silently rejects the file and you never know what went wrong. As can be derived from the question, an alternative approach would be:
// $zip->addFile($file);
$content = file_get_contents($file);
$zip->addFromString(pathinfo ( $file, PATHINFO_BASENAME), $content);
In addition to getting the code working, file_get_contents() also generates decent error messages.
The ZipArchive::addFile() method accepts the path to the file as its first parameter.
Here, you are using :
$zip->addFile("/trash-icon.png", "/gabage.png");
Which means you are trying to add the /trash-icon.png file to your archive.
Are you sure this file exists ?
Note there is a / at the beginning of that file's path, which indicates it's an absolute path.
Maybe that / should be removed, to use a relative path ?
I had similar kind of issue and it was related with the file that I was going to add to the zip archive.
Before adding file to zip archive, it's always better to check if the file exists.
$thisdir = "$_SERVER[DOCUMENT_ROOT]/zip";
if (file_exists($thisdir . "/trash-icon.png")) {
$zip->addFile($thisdir . "/trash-icon.png", "/gabage.png");
}