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 8308103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:41:48+00:00 2026-06-08T18:41:48+00:00

I have a wordpress site and I’m creating an HR plugin for the company

  • 0

I have a wordpress site and I’m creating an HR plugin for the company I work for. One function allows administrators to download a zip file containing several documents. I have tested the following code which works fine but I would like the ZIP archive to be created in a directory of my choice.

function direct_download()
{
    if ($_REQUEST['action'] == 'download')
    {
        $App = new DHR_Application();
        $file_name=str_replace(" ","_",$App->Fname)."_".str_replace(" ","_",$App->Lname)."_".$App->ID;
        $txt_name=$file_name.".txt";
        $txt_path="../user-upload/temp/".$file_name.".txt";
        $arc_name=$file_name.".zip";


        $txt=$App->Fname." ".$App->Lname."\r\n".$App->ID;
        $file=fopen($txt_path,"w+");
        fwrite($file,$txt);

        $zip = new ZipArchive();

        if ($zip->open($arc_name, ZIPARCHIVE::CREATE )!==TRUE)
        {
            exit("cannot open <$arc_name>\n");
        }
        $nameAtt1 = pathinfo($App->Att1,PATHINFO_BASENAME);
        $zip->addFile($App->Att1,$nameAtt1);
        if (($App->Att2 != '') && ($App->Att2 != null))
        {
            $nameAtt2 = pathinfo($App->Att2,PATHINFO_BASENAME);
            $zip->addFile($App->Att2,$nameAtt2);
        }

        $zip->addFile($txt_name,$txt_path);
        $zip->close();
        fclose($file);

        header("Content-type: application/zip");
        header("Content-Disposition: attachment; filename=$arc_name");
        header("Pragma: no-cache");
        header("Expire: 0");
        readfile("$arc_name");
        exit();
    }
}

Normally the ZIP archive is created in /wp-admin/ directory. I don’t want temporary files created and deleted in this directory because it runs the back end of wordpress.

When I include the path of the file in $arc_name the code breaks. I looked at the $zip->filename value and it will change the value from “C:/wamp/www/company/user-upload/temp/Tony_Beroni_000001.zip” to “C:\wamp\www\company\user-upload\\temp\Tony_Beroni_000001.zip”. I assume this could be an issue because of the difference between WAMP and a native Apache server(no pun intended).

Does anyone know how to create a ZIP archive with PHP in a specific directory?

Edit: I noticed that I had the parameters for $zip->addFile($txt_name,$txt_path); in reverse it should be $zip->addFile($txt_path, $txt_name);

  • 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-08T18:41:49+00:00Added an answer on June 8, 2026 at 6:41 pm

    If you are doing cross-platform development (i.e. Windows and Linux). You should alwasy specify paths using the PHP DIRECTORY_SEPARATOR constant like:

    $full_file_path = DIRECTORY_SEPARATOR . 'path' . DIRECTORY_SEPARATOR . 'to'. DIRECTORY_SEPARATOR . 'file' . DIRECTORY_SEPARATOR . $file_name;
    

    You can define the path however you like, so long as the running webserver has permissions to write to that path.

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

Sidebar

Related Questions

I have a Wordpress site. I want to embed a Flash file in one
I have a WordPress site which have 4 pages with one page is a
I have a wordpress site for ex, www.test.com There is a js file (forms.js)in
I have a Wordpress site that uses two databases -- one section queries one
I have a wordpress site with a plugin container. In the container there are
I have a wordpress site, and a plugin uses cufon text replacement. (I'm not
I have wordpress a site with a customized login page, the problem is when
i have an wordpress site and it have menus over there. what i want
I have a Wordpress site (installed in root) and a Simple Machines Forum (in
I have a wordpress site that I only want to allow people to post

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.