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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:40:22+00:00 2026-05-29T08:40:22+00:00

I am trying to get a foreach loop to give me the checkbox values

  • 0

I am trying to get a foreach loop to give me the checkbox values (which have the file names for values) from my form, to be added to a zip file.

<input type="checkbox" name="download[0]" value="Fact_Sheet_1.pdf" />
<input type="checkbox" name="download[1]" value="Fact_Sheet_2.pdf" />
<input type="checkbox" name="download[2]" value="Fact_Sheet_3.pdf" />
<input type="checkbox" name="download[3]" value="Fact_Sheet_4.pdf" />
<input type="checkbox" name="download[4]" value="Fact_Sheet_5.pdf" />

Works fine if I click on one checkbox but when I tick more than one checkbox only one file gets added to the zip file.

My code is below. Any suggestions would be appreciated.

if(isset($_POST['submit']) )
    {
    foreach ($_POST['download'] as $key => $val) {
        $filename = 'Fact_Sheets/' . $val;
        }
    }
system("zip -r files/zipfile  $filename"); 
  • 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-05-29T08:40:23+00:00Added an answer on May 29, 2026 at 8:40 am
    foreach ($_POST['download'] as $key => $val) {
        $filename = 'Fact_Sheets/' . $val;
    }
    system("zip -r files/zipfile  $filename"); 
    

    You overwrite $filename each time.

    Instead, build it up:

    $filenames = '';
    foreach ($_POST['download'] as $key => $val) {
        $filenames .= '"Fact_Sheets/' . $val . '" ';
    }
    system("zip -r files/zipfile  $filenames");
    

    (Also note that it’s conventional to name the inputs like "download[]", so that the indexing is done automatically for you. This is not crucial, though.)


    But don’t do this. As discussed in comments, it has a GAPING FLAW. Don’t write this code, even if you’re “just testing”, or even if you’ll “fix it later”. Don’t let it ever exist in the first place, or you will make a mistake, or accidentally leave it in place, or not find the time to do it properly.

    I considered not encouraging you by not writing this answer, but concluded that if I allowed some other answer to be accepted, it might be a dangerous answer that’d make things even worse.

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

Sidebar

Related Questions

I'm trying get values from a GridView using the following code: foreach (GridViewRow row
I am trying to get a 0 result from this foreach loop but it
I'm trying to use the foreach loop in an Ant script but I get
I am trying to call a method from a foreach loop on each iteration
I have a function which is called within a foreach loop and takes in
I'm trying to fill an array with my foreach loop, but I don't get
Trying to get this example working from http://www.munna.shatkotha.com/blog/post/2008/10/26/Light-box-effect-with-WPF.aspx However, I can't seem to get
Trying to get parameters from a PUT request using HttpServlet#doPut: public void doPut(HttpServletRequest request,
Trying to get comfortable with jQuery and I have encountered some sample code that
I'm trying to get simplepie to loop through a few rss feeds using codeigniter

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.