Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP

Jake White
Jake White
41,721 Points

PHP Downloads

I'm building an e-commerce store with php. I've followed the videos on how to build the products array and building the product page. The products that are going to be sold are going to be digital products, so Im putting a download button on the product page to download the corresponding .zip file. How would I actually get this to work? I would assume that I would need to but the download path within the product array, but how would I get the button, once clicked, to download the .zip file from the array? Or am I looking at this all wrong?

Any help would be appreciated.

11 Answers

Ian Carroll
Ian Carroll
1,901 Points

I don't know if the file is unique per user, but based off of what I read:

create a file in the directory of the store called zip Put your downloads in that directory Then just put a <a type="button" href="/ecommerce/zip/file.zip">Download Me</a>

This has it's security flaws, change the filename every so often.

Jake White
Jake White
41,721 Points

What Im needing though follows along with the product arrays that were taught in the php classes. I have my product arrays all set. When you click on the image, it takes you to the product page for that product. I need for the download button on the product page to download the zip file for that product. Since Im using one product page template for each item, how can i configure the download button to grab the matching file?

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

I would zip up all the assets associated with a single product into a single zip file. I would then put the web address for that zip file as an element of the array, just like the img element has the path to the image. Then you can create a link to that zip file that will download the zip file. Each product detail page shows a different image; in the same way, you want each product detail page to link to a different zip file.

It sounds like that's what you are doing: that approach sounds correct. Are you having trouble getting it to work?

Jake White
Jake White
41,721 Points

This is how i have the array set up: $stills[Genesis] = array( "name" => "Genesis", "img" => "Stills/Books/Genesis/Genesis(16-9).jpg", "verse" => "A Study of the Book of Genesis", "content" => "Stills/Books/Genesis/Genesis-content.jpg", "blank" => "Stills/Books/Genesis/Genesis-blank.jpg", "download" => "www.beta.teamalabaster.com/Stills/Books/Genesis/Genesis.zip", );

Then with my button I have this: <a href="<?php $still['download'] ?>"><button class="download" type="submit">Download</button></a>

Maybe I'm not quite getting it...

Jake White
Jake White
41,721 Points

Oh...thats weird... I meant to paste what my button tag shows of href="<?php $still['download'] ?>"><button class="download" type="submit">Download</button>

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

(Use four spaces in front of a line here in the forum to make it treated like code.)

$stills[Genesis] = array(
    "name" => "Genesis",
    "img" => "Stills/Books/Genesis/Genesis(16-9).jpg",
    "verse" => "A Study of the Book of Genesis",
    "content" => "Stills/Books/Genesis/Genesis-content.jpg",
    "blank" => "Stills/Books/Genesis/Genesis-blank.jpg",
    "download" =>   "www.beta.teamalabaster.com/Stills/Books/Genesis/Genesis.zip",
    );

I think you need an absolute URL, with http:// in front of it.

    "download" =>   "http://www.beta.teamalabaster.com/Stills/Books/Genesis/Genesis.zip"
Jake White
Jake White
41,721 Points

So I added the http:// and it still didn't work. Im thinking maybe its my button?

<a href="<?php $still['download']; ?>"><button class="download" type="submit">Download</button></a>
Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

Remove the button tag; the link alone is all you need.

Jake White
Jake White
41,721 Points

so removing the button tag didn't work either... I tried it these two ways

<a href="<?php $still['download']; ?>">Download</a>

and

<?php <a href="$still['download'];">Download</a> ?>

that second one returns a syntax error

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

The first one is correct, but you have to echo out the value. If you were to look at the HTML for the first one, it would look like this:

<a href="">Download</a>

I always recommend looking at the HTML using View Source or Chrome Inspector to see if there's something wrong with the generated output. A blank href clearly won't work.

You need the PHP to look like this, with an echo statement:

<a href="<?php echo $still['download']; ?>">Download</a>

It seems like everything else is in order; I suspect that will work.

Jake White
Jake White
41,721 Points

RANDY!!! YOU'RE THE MAN!!!

Definitely my favorite teacher ever! Tell Carson you deserve a raise! Thank you so much! The echo command worked. I had thought about trying that, but thought to myself "No that won't work you idiot...don't waste you're time."

Thanks so much for taking the time to help me.