Page 1 of 1

Trouble Zipping files!

Posted: Wed Jan 27, 2010 6:51 pm
by George_Cy
I'm trying to zip up some folders and it's contents so people can download them!
I'm actually working on Wamp and on windows, because it's a school project.

So my function works, but it zips up, the correct files, but it includes the whole path.
for example C:\wamp\www\uploads\Csc290\Assignment1\(and the files in here)
all i want is just the folder Assignment1 + the files in there! the folder Csc290 and Asssignment1 are names from the database!


Here is my code,

Code: Select all

 
if($instr== "false"){
// Set path to download zip file
$dld = $row['asgnname'].".zip";
$dlddir = "./uploads/".$row2['coursename']."/".$row['asgnname']."/";
$dldlink = "./uploads/".$row2['coursename']."/".$row['asgnname'].".zip";
}else{
$dld = "instructions-".$row['asgnname'].".zip";
$dlddir = './uploads/'.$row2['coursename'].'/'.$row['asgnname'].'/instructions/';
$dldlink="./uploads/".$row2['coursename']."/instructions-".$row['asgnname'].".zip";
}
 
function Zip($source, $destination)
{
    if (extension_loaded('zip') === true)
    {
        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();
        }
    }
    return false;
}
 
zip($dlddir,$dldlink);
 

Anyone got an idea how to fix this?