I am开发者_开发技巧 currently trying to create a content uploading system and although there are error being made from the page when I check the appropriate folder for the contents it is empty
$chapterZip = new ZipArchive();
if ($chapterZip->open($_FILES['chapterUpload']['name']))
{
for($i = 0; $i < $chapterZip->numFiles; $i++)
{
$pictureName = $chapterZip->getNameIndex($i);
$fileOpened = $chapterZip->getStream($pictureName);
if(!$fileOpened) exit("failed\n");
while (!feof($fileOpened)) {
$contents = fread($fileOpened, 8192);
// do some stuff
if(copy($contents,"Manga/".$_POST['mangaName']."/".$_POST['chapterName']."/".$pictureName.""))
{
if(chmod("Manga/".$_POST['mangaName']."/".$_POST['chapterName']."/".$pictureName."", 0664))
{
$errmsg0.= "File successfully copied<br/>";
}
else
{
$errmsg0.= "Error: failed to chmod file<br/>";
}
}
else
{
$errmsg0.= "Error: failed to copy file<br/>";
}
}
fclose($fileOpened);
}
}
Any help with this problem would be much appreciated
I looked into it further and found a fairly simple method to extract file with on the PHP online manual
$zip = new ZipArchive;
$res = $zip->open('test.zip');
if ($res === TRUE) {
echo 'ok';
$zip->extractTo('test');
$zip->close();
} else {
echo 'failed, code:' . $res;
}
I don't know if this helps you, but I wrote this script yesterday after some info I found searching for zipping recursively.
function zip($source, $destination)
{
if (file_exists($source) === true)
{
$zip = new ZipArchive();
if ($zip->open($destination, ZIPARCHIVE::CREATE) === true)
{
$source = realpath($source);
if (is_dir($source) === true)
{
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
foreach ($files as $file)
{
$file = realpath($file);
if (is_dir($file) === true)
{
$zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
}
else if (is_file($file) === true)
{
$zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
}
}
}
else if (is_file($source) === true)
{
$zip->addFromString(basename($source), file_get_contents($source));
}
}
return $zip->close();
}
}
Found it on some website I can't remember where. Adapted it a slight bit.
Usage: zip('mydir/', 'myZipFile.zip');
Br, Paul Peelen
精彩评论