How to create compressed file using PHP with destination directory?
<p>I found the script below from previous posts to create a ZIP file using PHP.</p>

<p>This script lets you create a zip file, but it doesn't let you create it in any directory you  want. This is the original code. </p>

<pre><code>    &lt;?php
    // Config Vars 

    $sourcefolder =   "uploads/output" ; // Default: "./" 
    $zipfilename  = "myarchive.zip"; // Default: "myarchive.zip"
    $timeout      = 5000           ; // Default: 5000

    // instantate an iterator (before creating the zip archive, just
    // in case the zip file is created inside the source folder)
    // and traverse the directory to get the file list.
    $dirlist = new RecursiveDirectoryIterator($sourcefolder);
    $filelist = new RecursiveIteratorIterator($dirlist);

    // set script timeout value 
    ini_set('max_execution_time', $timeout);

    // instantate object
    $zip = new ZipArchive();

    // create and open the archive 
    if ($zip-&gt;open("$zipfilename", ZipArchive::CREATE) !== TRUE) {
        die ("Could not open archive");
    }

    // add each file in the file list to the archive
    foreach ($filelist as $key=&gt;$value) {
        $zip-&gt;addFile(realpath($key), $key) or die ("ERROR: Could not add file: $key");
    }

    // close the archive
    $zip-&gt;close();
    echo "&lt;br&gt;Archive ". $zipfilename . " created successfully - ";

    // And provide download link ?&gt;
    &lt;a href="http:&lt;?php echo $zipfilename;?&gt;" target="_blank"&gt;Download &lt;?php echo $zipfilename?&gt;&lt;/a&gt; 
</code></pre>

<p>I tried amending the code to be able to decide the desitnation of the script as I found no solution after spending a couple of hours on the internet. Some solutions suggested where to change $zipfilename  = "myarchive.zip"; to $zipfilename  = "uploads/output/myarchive.zip"; but in the URL to download link I want to appear as: <strong>Archive myarchive.zip created successfully - Download myarchive.zip</strong> not as <em>Archive uploads/output/myarchive.zip created successfully - Download myarchive.zip</em></p>

<p>Therefore I tried this code, but it didn't work for me. I don't know what I did wrong.</p>

<pre><code>&lt;?php
// Config Vars 

$sourcefolder =   "uploads/output" ; // Default: "./" 
$destfolder  = "uploads/output/"; // Default: "myarchive.zip"
$zipfilename  = "myarchive.zip"; // Default: "myarchive.zip"
$timeout      = 5000           ; // Default: 5000

// instantate an iterator (before creating the zip archive, just
// in case the zip file is created inside the source folder)
// and traverse the directory to get the file list.
$dirlist = new RecursiveDirectoryIterator($sourcefolder);
$filelist = new RecursiveIteratorIterator($dirlist);

// set script timeout value 
ini_set('max_execution_time', $timeout);

// instantate object
$zip = new ZipArchive();

// create and open the archive 
if ($zip-&gt;open(''.$destfolder.''.$zipfilename.'', ZipArchive::CREATE) !== TRUE) {
    die ("Could not open archive");
}

// add each file in the file list to the archive
foreach ($filelist as $key=&gt;$value) {
    $zip-&gt;addFile(realpath($key), $key) or die ("ERROR: Could not add file: $key");
}

// close the archive
$zip-&gt;close();
echo "&lt;br&gt;Archive ". $zipfilename . " created successfully - ";

// And provide download link ?&gt;
&lt;a href="http:&lt;?php echo $zipfilename;?&gt;" target="_blank"&gt;Download &lt;?php echo $zipfilename?&gt;&lt;/a&gt; 
</code></pre>
