Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Problem with SS_HTTPRequest::send_file


Reply


2 Posts   720 Views

Avatar
_Vince

Community Member, 162 Posts

19 January 2012 at 11:57pm

I'm having a problem with SS_HTTPRequest::send_file

I am trying to do the following:

Customers can buy .mp3 files. When they buy one, they receive a code or password and a link via email.

When the customers click on the link, the system checks whether the code is valid, works out what file to serve and, in theory, serves it. I don't want to have a direct link to the assets folder.

So, I have this in my .config.php to send everything to a class where I can check everything

Director::addRules(100, array(
'SecureFiles//$Action/$ID' => 'SendSecureFile'
));

and this in the .htaccess in my assets folder

# Secure files
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} !(\.gif$)|(\.jpg$)|(\.png$)
RewriteRule ^assets/(.*)$ SecureFiles/%1 [L]

Finally I have a SendSecureFile class that reads the product id and the associated password/code

<?php

class SendSecureFile extends Controller {

function index() {

//pw = password
// pr = product

if(!isset($_GET["pw"]) || ($_GET["pw"]=="") || !isset($_GET["pr"]) || ($_GET["pr"]=="")){
return ErrorPage::response_for(400);
}
else{

$sqlQuery = new SQLQuery();
$sqlQuery->select = array("f.Filename", "f.Name", "FileNameID");
$sqlQuery->from = array("
   Product p
   LEFT JOIN Product_OrderItem pi ON (p.ID = pi.ProductID)
   LEFT JOIN File f ON (FileNameID = f.ID)
   ");
$sqlQuery->where = array("pi.ProductPassword = '" . $_GET["pw"] . "' AND p.ID = " . $_GET["pr"]);

$result = $sqlQuery->execute();

if($sqlQuery->unlimitedRowCount() == 0){
//no matches
    return ErrorPage::response_for(404);
   }

foreach($result as $row) {
//should only be one, really...
   $fileName = $row['Filename'];
   $Name = $row['Name'];
}

$fullPath = Director::BaseFolder() . "/assets/Uploads/" . $Name;
         
if (!file_exists($fullPath) || is_dir($fullPath) ) {
// File really not found - must be a discrepancy with the database
Debug::show("$fullPath not found");
    return ErrorPage::response_for(500); //set to something better later.
}

// Send file
   return SS_HTTPRequest::send_file(file_get_contents(Director::absoluteBaseURL() . $fileName), Director::absoluteBaseURL() . $fileName);

      }
}

}

And here is my problem:

The file doesn't get sent, instead, I get the file content on my screen, which freezes the browser.

What am I doing wrong?

I want to get the usual dialog where you are asked if you want to save the file or play it.

Thanks in advance.

Avatar
Reflektera

49 Posts

22 September 2012 at 8:49am

Kind of stuck at the same place as you, did you solve this problem?