Jump to content

Recommended Posts

Hi i'm new to phpfreaks and i need some help on zipping a folder!

i work on a project under windows, using Wamp.

i use the function posted underneath and it works, but, when i unzip the zip file, in it it contains the whole path

i.e. C:\wamp\www\uploads\Csc290\(+ the files).

Can anyone guide me how to get only a folder with Csc290 and the files included in it?

 

any help or tips will be appreciated! thnx

 

i get the names of the folders through the database and use them as input to the function

 

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);

Link to comment
https://forums.phpfreaks.com/topic/189796-zip-folder/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.