I'm working on a customization of the "Samples" block on Products. Basically what I need to know is how to get the file name from a sample.

So, I can use the following code in my view to get an array of samples:

<?php $_samples = $this->getSamples() ?>

... and I can iterate over those samples well enough. What I'm trying to figure out, is, how can I get the name of the file associated with that sample? I basically want to do a customization to the view where, if the sample file is an MP3, I display an embedded player. For all other file types, the default behavior (a link is shown, which when clicked, opens a new window and displays the sample) is perfectly fine.


So, if you concatenate $samplePath and $sampleFile together, you get the full path the the sample file, on disk. i.e.: $samplePath . $sampleFile gets you /path/to/samples/folder/sub-folder/path/to/file.mp3

In this example, it's actually not necessary to get the $samplePath, since you can just test whether or not $sampleFile ends with ".mp3", as shown below, but I wanted to demonstrate how to get to the full path anyhow.

if ($this->hasSamples()):
    $_samples = $this->getSamples();

    foreach ($_samples as $_sample):
        $samplePath = $_sample->getBasePath(); // returns "/path/to/samples/folder"
        $sampleFile = $_sample->getSampleFile(); // returns "/sub-folder/path/to/file.mp3"

        // Figure out if the filename ends with ".mp3"
        $_is_mp3 = (bool)(".mp3" == substr($sampleFile, -4));
        if ($_is_mp3):
            //show the mp3 player
            //show a link to the file


| improve this answer | |
  • I'll give this a shot tomorrow and let you know. Looks like the getSampleUrl() and/or getSamplePath() will get me what I need. Thanks! – jefflunt May 22 '11 at 16:22
  • This got me headed in a good direction, but the best I could do with the methods mentioned above, as well as other ones that I found in the API, was get the base directory for the samples, e.g. /path/to/app/samples, rather than /path/to/samples/1/sample.mp3. I may just have to add this functionality. Luckily (I think), Magento is open source. – jefflunt May 23 '11 at 14:56
  • Does the getURL() function not return a full path to the file? If so, you can either trim the string up to the dot before the file extension to determine the file type, or use this solution if the full file path returned by getURL() points to a download ID rather than the file itself. – Nick May 23 '11 at 15:56
  • Nope, the getURL() function returns the URL (e.g. example.com/samples/01), which (a) doesn't include the filename, and (b) just delegates to your browser to handle the view/playback of the sample appropriately. For example, the URL, if the sample is a PDF, will launch whatever your default PDF viewer is. In the case of .MP3s, for me the URL was launching my default media player, locally on my machine. – jefflunt May 23 '11 at 16:05
  • So I modified your answer to include the working code. I don't know if you have to approve it before it shows up, but you definitely got me going in the right direction, and I found the code that does it thanks to your help. – jefflunt May 23 '11 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.