Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8232945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:58:54+00:00 2026-06-07T17:58:54+00:00

I have the following zip download function: $file=’myStuff.zip’; function downloadZip($file){ $file=$_SERVER[DOCUMENT_ROOT].’/uploads/’.$file; if (headers_sent()) {

  • 0

I have the following zip download function:

$file='myStuff.zip';
function downloadZip($file){
  $file=$_SERVER["DOCUMENT_ROOT"].'/uploads/'.$file;
   if (headers_sent()) {
    echo 'HTTP header already sent';
   } 
       else {
        if (!is_file($file)) {
            header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found');
            echo 'File not found';
        } else if (!is_readable($file)) {
            header($_SERVER['SERVER_PROTOCOL'].' 403 Forbidden');
            echo 'File not readable';
        } else {
            header($_SERVER['SERVER_PROTOCOL'].' 200 OK');
            header("Content-Type: application/zip");
            header("Content-Transfer-Encoding: Binary");
            header("Content-Length: ".filesize($file));
            header("Content-Disposition: attachment; filename=\"".basename($file)."\"");
            readfile($file);
            exit;
        }
    }
}

The problem is when I call this function, I end up downloading not just myStuff.zip, but the complete directory path with all the folders. I’m on a Mac using XAMPP so this means I get the following:

/applications/xampp/htdocs/uploads/myStuff.zip

meaning i get a folder called applications with all the subfolders and then inside all of them I get myStuff.zip.

How can I just download myStuff.zip without its directories?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T17:58:55+00:00Added an answer on June 7, 2026 at 5:58 pm

    Ok, I answered my own question by using the code in this link: http://www.travisberry.com/2010/09/use-php-to-zip-folders-for-download/

    Here’s the PHP:

    <?php
    //Get the directory to zip
    $filename_no_ext= $_GET['directtozip'];
    
    // we deliver a zip file
    header("Content-Type: archive/zip");
    
    // filename for the browser to save the zip file
    header("Content-Disposition: attachment; filename=$filename_no_ext".".zip");
    
    // get a tmp name for the .zip
    $tmp_zip = tempnam ("tmp", "tempname") . ".zip";
    
    //change directory so the zip file doesnt have a tree structure in it.
    chdir('user_uploads/'.$_GET['directtozip']);
    
    // zip the stuff (dir and all in there) into the tmp_zip file
    exec('zip '.$tmp_zip.' *');
    
    // calc the length of the zip. it is needed for the progress bar of the browser
    $filesize = filesize($tmp_zip);
    header("Content-Length: $filesize");
    
    // deliver the zip file
    $fp = fopen("$tmp_zip","r");
    echo fpassthru($fp);
    
    // clean up the tmp zip file
    unlink($tmp_zip);
    ?>
    

    and the HTML:

    <a href="zip_folders.php?directtozip=THE USERS DIRECTORY">Download All As Zip</a>
    

    The key step in getting rid of the directory structure appears to be the chdir(). It is also worth noting that the script in this answer makes the zip file on the fly rather than trying to retrieve a previously zipped file as I did in my question.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have downloaded the following zip. http://www.palmmedia.de/Blog/Download/f0f3664d-3199-4095-bb15-815cf9f3ef2e I did update the MVCSitemap packige in
Currently to have to user download a file I have the following javascript. document.location.href
I've download eclipse-jee-galileo-win32.zip and following instruction of this post, http://blog.kukiel.net/2009/09/coldfusion-on-google-app-engine-with.html After that, I encounter
I have the following table called stores id store zip 1 Market 1 01569
I have the following code for adding to/extracting from Zip. I'm trying to refactor
I have the following task in my MSBuild script: <Target Name=ZipStates> <Message Text=CREATING ZIP
I have an html page with the following content: [...] <tr><td class=n><a href=play-1.0.1.zip>play-1.0.1.zip</a></td></tr> <tr><td
I have following plist: <?xml version=1.0 encoding=UTF-8?> <!DOCTYPE plist PUBLIC -//Apple//DTD PLIST 1.0//EN http://www.apple.com/DTDs/PropertyList-1.0.dtd>
I have following fiddle: http://jsfiddle.net/BFSH4/ As you see there are two issues: The h1
I have the following mapping in my Castle Windsor xml file which has worked

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.