Jump to:

22977 Posts in 11806 Topics by 2826 members

General Questions

SilverStripe Forums » General Questions » Problem with SS_HTTPRequest::send_file

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

Page: 1
Go to End
Author Topic: 584 Views
  • _Vince
    Avatar
    Community Member
    156 Posts

    Problem with SS_HTTPRequest::send_file Link to this post

    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.

  • Reflektera
    Avatar
    49 Posts

    Re: Problem with SS_HTTPRequest::send_file Link to this post

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

    584 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.