Posted on: 12:00 am Sep 7 2010 By: tectonicz
How to integrate zip to my upload script, it currently just supports regular files.
This is what currently happends, you upload a file if the file matches any of the preg_matches it runs some code on the $file, and then does a header download (downloads the file after the code is run).
What im trying to get it to is, to also allow zip uploads, and apply the same rule, (but too as many files contained within the zip).
PHP Code:
By tectonicz on 5:53 pm Oct 31 2009:
I found a few tutorials, after googling, but I don't understand how to integrate them into my situation:
http://www.java-samples.com/showtutorial.php?tutorialid=985
http://net.tutsplus.com/videos/screencasts/how-to-open-zip-files-with-php/
:/
By Aziz on 6:22 pm Oct 31 2009:
You can try using zip open on the $_FILES['file']['tmp_name']. You can see how its done here:
http://www.php.net/manual/en/function.zip-open.php
By tectonicz on 6:49 pm Oct 31 2009:
Thanks, but that function is complex, can you show me how to do it?, please
I tried adding the following lines at the top after the submit if...
$zip = zip_open($_FILES['file']['tmp_name']);
$file = zip_read($zip);
But no luck.
This is how i want it to be:
If its a single file (.php): (Current does this)
1. Check if the file contains the preg_matches, if it does proceed to step 2, if it dont proceed to step 3.
2. Str_replace, then download the file
3. Do nothing.
If its a zip (archive of files): (Trying to add this)
1. Check if the zip files contains the preg_matches, if they do proceed to step 2, if it dont proceed to step 3
2. Str_replace on the files containing the matches, then download the new zip containing the replaces.
3. Do nothing.
By Aziz on 12:48 pm Nov 1 2009:
What if one files contain a match and the others don't, etc..?
By tectonicz on 1:42 pm Nov 1 2009:
Then just effect that specific file, and ignore the rest.
Example:
I upload a zip containing (input):
- file1.php
- file2.php <<is MATCH
- file3.php
The downloaded zip would contain (output):
- file1.php
- file2.php <<is EFFECTED/STR_REPLACED
- file3.php
Like this site has: http://www.fopo.com.ar/ (upload a zip containing php to test)
By tectonicz on 6:43 pm Nov 2 2009:
Anyone?, please :/
By Aziz on 10:17 am Nov 10 2009:
Sorry tectonicz, been very very extra busy lately.
Here is how I would do it:
after
if($submit == "Submit") {
I'll add another statement to check if it is a file or a zip file:
if($_FILES['file']['type'] == 'application/zip')
{
// our code
} else {
// previous code you had
}
second step is //our code:
here is the pseudo code I would implement:
$randomDirectory = createRandomDirectoryFunction();
$extractedFilesList = extractZipFile($_FILES['file']['tmp_name'],$randomDirectory);
foreach($extractedFilesList as $fileName)
{
$file = file_get_contents($fileName);
// check for preg_match like you did previously to each file, replace if necessary
}
// Now all files changes, zip them again - all files in the directory
$newZipFile = zipFiles($extractedFileList);
// Done remove directory
rmdir($randomDirectory);
// Remove zipfile
unlink($newZipFile)
// You now only have to loop online for a function or class to extract the files from the zip file and put them back in.
By tectonicz on 7:08 pm Nov 11 2009:
No problem,
// You now only have to loop online for a function or class to extract the files from the zip file and put them back in.
I googled and couldnt find any easy classes, i found this though:
http://www.phpclasses.org/browse/file/7539.html
But not sure how to integrate it or can you perhaps suggest me a better class?
and what does this mean:
$randomDirectory = createRandomDirectoryFunction();
Thanks
By tectonicz on 9:21 pm Nov 16 2009:
Sorry for bothering you again, but can you help me?
thanks.
By Aziz on 9:14 am Nov 17 2009:
Hello,
You can integrate that class for creating the zip package after checking for matches. As specified in the example, it's not hard:
include('class.easyzip.php');
$z = new EasyZIP;
$z -> addFile("map.bmp");
$z -> addFile("guide.pdf");
$z -> addDir("files/test");
$z -> zipFile("xyz.zip");
You can use this function to extract the files (by strefrextor [at] gmai l.com)
function ezip($zip, $hedef = '')
{
$root = $_SERVER['DOCUMENT_ROOT'];
$zip = zip_open($root . $zip);
while($zip_icerik = zip_read($zip)):
$zip_dosya = zip_entry_name($zip_icerik);
if(strpos($zip_dosya, '.')):
$hedef_yol = $root . $hedef . 'x/'.$zip_dosya;
touch($hedef_yol);
$yeni_dosya = fopen($hedef_yol, 'w+');
fwrite($yeni_dosya, zip_entry_read($zip_icerik));
fclose($yeni_dosya);
else:
@mkdir($root . $hedef . 'x/'.$zip_dosya);
endif;
endwhile;
}
ezip('files.zip', 'unzip_files/');
Finally, the createRandomDirectoryFunction() can be something like:
function createRandomDirectoryFunction()
{
return substr(md5(uniqid(rand(),1)),1,5);
}
By tectonicz on 11:47 am Nov 17 2009:
Ok I've integrated your code into my code (attached).
But still does not function?, am i missing something, or doing anything incorrectly?
PHP Code:
By Aziz on 9:57 am Nov 20 2009:
There is a small problem, you are reading the file, replacing the text, but not writing it back.
If you preg_match a female, after str_replace, write the $file content to the file again. Otherwise, you would be just writing the old contents.
By tectonicz on 10:30 am Nov 20 2009:
How would i write?
By Aziz on 9:59 am Nov 21 2009:
To write to a file, you will use fopen with the "w" option:
http://php.net/manual/en/function.fopen.php
By tectonicz on 10:45 pm Nov 27 2009:
Grrrrr, still no luck - im feeling stupid.
Heres my current code:
PHP Code:
By Aziz on 1:30 pm Feb 16 2010:
Sorry for the late response, in the event you are still having problem, you need to edit the ezip function:
Remove the two instances of 'x/' from the code.
Change this line:
$extractedFilesList = ezip($_FILES['file']['tmp_name'],$randomDirectory);
To:
ezip($_FILES['file']['tmp_name'],$randomDirectory);
$extractedFilesList = glob("$randomDirectory/*");
Hopefully, this will give you the correct files.