Generating files/folders? - php

This might be a horrifically stupid question, but how would I go about doing this in PHP:
I'd like to make a .zip file dynamically, then serve it as a download immediately with force-download mime or something, with the contents of $string, with a folder structure.
Premade folder structure license\keys\{$_SERVER['REMOTE_ADDR']}\{$fn}.cf and {$fn}.cf would contain the contents of $string, inside the .zip file
I have literally no idea where to start.

You could use the ZipArchive class. Use it to create a temporary ZIP file, close the ZipArchive, stream the temporary ZIP file to the user, and delete the temporary ZIP.

Related

Move file from temporary folder to uploads folder

In my WordPress the visitors can upload files like texts and this files go in this path
C:\xampp\htdocs\www\wordpress\wp-content\uploads\temporary, After validating I need to insert this files in this path C:\xampp\htdocs\www\wordpress\wp-content\uploads\2014\10 like attachment , Should I using wp_insert_attachment or wp_handle_upload() ?
I don't know if this is related to wordpress because I do not have a slightest idea about it. But in php, you use the copy() function to copy a file from one folder to another. And if you want to remove the file from the temporary folder, you are looking for the unlink() function for deleting a file

PHP ZipArchive can't read more then 21797 files inside

I need to extract from zip file data.zip only one file for example 188139.xml
File contains folder with more than 88000 files. But after open - it shows me 21797 files and can't open file with big index (which is truly there). But opens 1.xml, 200.xml etc.
So it looks like limitation. Is there any suggestions how to open needed file?
Looks like it a bug of library. Fixed with alternative use of execute() function.

ci zip encoding class not working properly

I want to crate a zip file and download it.
So my code is as follow;
Since i am using ci zip encoding class
my folder structure is like
c:/xampp/htdocs/gallery/print/oid_1/1.jpg,2.jpg,3.jpg
My code
$path = getcwd().'/print/oid_1/';
$this->zip->read_dir($path,FALSE);
// Download the file to your desktop. Name it "my_backup.zip"
$this->zip->download('oid_1.zip');
Here is my problem,
l C:\Users\instinctclassic\Downloads\Compressed\oid_1.zip: The archive is either in unknown format or damaged so what does this means -does it means it was mistaken while making zip file or it was mistaken while downloading.For downloading mistake i have downloaded the zip file many time enough to be sure.
2 I repaired the downloaded zip file(oid_1) and extract it but the extracted folder structure is not ignored before the oid_1 folder as said in ci zip encoding class tutorial
$this->zip->read_dir($path, FALSE);
//This will create a ZIP with the folder "directory" inside, then all sub-folders stored correctly inside that, but will not include the folders /path/to/your.
3 Assuming my folder oid_1 already exists somewhere on server.
I know this question is previously asked but mine exist all the problem from making zip file to extracting.So i am sorry for this.
Thanks
Try directory path like this:
$path = getcwd().'\print\oid_1\';
this way path will be proper, because getcwd() will return you c:\xampp\htdocs\gallery

Codeigniter Zip Library how to zip existent file

Im moving un-zipped files to a server directory /zip/file.unzipped.png
now i need to take that file and zip it.
Is it possible to do that or a need the zip archive as codeigniter guide says?
How can i do that?
Is what you're looking for just a straight download, and not saving the archive on disk before download?
If this is the case you can do what #zer02 suggested, but remove the archive step.
$name = 'mydata1.txt';
$data = 'A Data String!';
$this->zip->add_data($name, $data);
// Write the zip file to a folder on your server. Name it "my_backup.zip"
$this->zip->archive('/path/to/directory/my_backup.zip');
// Download the file to your desktop. Name it "my_backup.zip"
$this->zip->download('my_backup.zip');
The zip library should gather up all the data you've added to the zip cache in memory, and create a file to download dynamically.
Please read the user manual: http://codeigniter.com/user_guide/libraries/zip.html
There are two functions that jump out me for your use case:
$this->zip->read_file()
Permits you to compress a file that already exists somewhere on your
server. Supply a file path and the zip class will read it and add it
to the archive:
$this->zip->read_dir()
Permits you to compress a folder (and its contents) that already
exists somewhere on your server. Supply a file path to the directory
and the zip class will recursively read it and recreate it as a Zip
archive. All files contained within the supplied path will be encoded,
as will any sub-folders contained within it.
$name = 'mydata1.txt';
$data = 'A Data String!';
$this->zip->add_data($name, $data);
// Write the zip file to a folder on your server. Name it "my_backup.zip"
$this->zip->archive('/path/to/directory/my_backup.zip');
// Download the file to your desktop. Name it "my_backup.zip"
$this->zip->download('my_backup.zip');
Zip Lib CI

Get a ZIP and extract contents OR file_get_contents individual files and then file_put_contents into folder

I'm writing a script that will grab several files and then copy them into a new directory. I've come up with two methods of doing this but want to know which is better.
1)
store several file names in an array and use file_get_contents in a loop to get those files.
same again, but using file_put_contents to copy these files to a new directory.
2)
Store these files needed in a .ZIP file by default
Use PHP to open this zip and extract the contents to the new folder.
I'm guessing that the ZIP method is better to use but I have no evidence to back that up. Can anyone advise me on what's best?
Store the files to copy in an array and use PHP's copy() function to copy the files to the desired directory.

Categories