Forum

Thread tagged as: Problem, Runway, Shop

Digital downloads - get correct file format

Hello,

I have an eshop that sells digital products, mostly audio tracks but also pdfs. I managed to create a secure bucket and give access to the files within only to people who buy those products. The problem is that when downloading the files I cannot seem to be able to get their format too. Of course I could just change the deliver url to something.mp3 but that wouldn't help if the file format is pdf. Is there any way I can get the file format into the "deliver" url?

My 'deliver' page code

<?php

      // config
      $url_param      = 'file';
      // Check a member is logged in and has purchased the file
      if (perch_member_logged_in()) {
          echo perch_get($url_param);
        if (perch_shop_customer_has_purchased_file(perch_get($url_param))) {
          perch_shop_download_file(perch_get($url_param));
        }
      }
    exit;
    ?>

Template code for downloading files

<a download href="/shop/purchased-files/deliver/<perch:shop id="fileID" />">Download</a>

URL pattern for deliver page

shop/purchased-files/deliver/[i:file]

I know there should be the file format in the url pattern but I cannot seem to get it right

Thanks for your help

Bob Gentle

Bob Gentle 0 points

  • 4 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Do you have the finfo extension installed in PHP?

I would say so, the extension is apparently enabled by default as of PHP 5.3.0. Our server is running on 5.6.27.

Drew McLellan

Drew McLellan 2638 points
Perch Support

You'd be surprised. Check. It's in the diagnostics report.

I found it as fileinfo, would that be the same thing?


HOSTING SETTINGS PHP: 5.6.27 Zend: 2.6.0 OS: Linux SAPI: apache2handler Safe mode: not detected MySQL client: mysqlnd 5.0.11-dev - 20120503 - $Id: 76b08b24596e12d4553bd41fc93cccd5bac2fe7a $ MySQL server: 5.5.53-MariaDB Free disk space: 792.64 GB Extensions: Core, date, ereg, libxml, openssl, pcre, sqlite3, zlib, bcmath, bz2, calendar, ctype, curl, dom, enchant, fileinfo, filter, ftp, gd, gettext, hash, iconv, SPL, intl, json, mbstring, mcrypt, session, mysql, mysqli, pgsql, standard, Phar, Reflection, mysqlnd, SimpleXML, soap, sockets, imap, tokenizer, xml, xmlreader, xmlwriter, xsl, zip, apache2handler, homeloader, PDO, pdo_sqlite, pdo_mysql, imagick, uploadprogress, memcache
Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, that's the same thing.

Thanks for your answers so far Drew, but I am quite confused right now. How can I use that to determine the file format when user goes to download the file?

Sorry if I am asking stupid questions and missing something obvious.

Drew McLellan

Drew McLellan 2638 points
Perch Support

FIleinfo is used for determining the file type - so you should be set.

I'm unclear if the issue is with files being uploaded with the wrong type or downloaded with the wrong type. Could you clarify?

Right now, after a user purchases a file, and wants to download it, the download link goes to shop/purchased-files/deliver/[i:file], which turns into shop/purchased-files/deliver/2 for example. This however downloads a file 2.html instead of 2.mp3 for example. Hope this makes it a bit clearer

Drew McLellan

Drew McLellan 2638 points
Perch Support

Are you calling perch_shop_download_file() before any HTML is sent to the browser?

Yes,

this is the only code thats on the deliver page

<?php

      // config
      $url_param      = 'file';
      // Check a member is logged in and has purchased the file
      if (perch_member_logged_in()) {
          echo perch_get($url_param);
        if (perch_shop_customer_has_purchased_file(perch_get($url_param))) {
          perch_shop_download_file(perch_get($url_param));
        }
      }
    exit;
?>
Drew McLellan

Drew McLellan 2638 points
Perch Support

Is it the same for all browsers?

Just tested it in Chrome and Firefox and yes, it's the same for both

Drew McLellan

Drew McLellan 2638 points
Perch Support

When you view the files in the control panel, are they marked as the correct type?

Yes, if I go to Assets - > Select the asset and edit it, the link to the file is for example /shop/purchased-files/deliver/picture.png or /shop/purchased-files/deliver/song.mp3

Drew McLellan

Drew McLellan 2638 points
Perch Support

Are they labelled with the right file type?

This is what I see

filetype

Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok, so it looks like the correct file type is being detected on upload.

Can you make a download request for the headers with curl and see what you get?

curl -I https://example.com/yourpage.php

Ok, this is what I get when trying to access one of the files

HTTP/1.1 200 OK
Date: Wed, 23 Nov 2016 13:40:43 GMT
Server: Apache
X-Frame-Options: deny
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
Set-Cookie: PHPSESSID=c04c30d955672120802dacee91d1c6c5; expires=Mon, 28-Nov-2016 13:40:43 GMT; Max-Age=432000; path=/; HttpOnly
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Set-Cookie: PHPSESSID=c04c30d955672120802dacee91d1c6c5; expires=Mon, 28-Nov-2016 13:40:43 GMT; Max-Age=432000; path=/; httponly
Content-Type: text/html; charset=utf-8
Drew McLellan

Drew McLellan 2638 points
Perch Support

Can you do the same now for the actual download link?

That was the download link that's being output by perch_shop_purchased_files();

Do you mean something else?