Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Sending files with XSendFile

by rpnoble419 (Pilgrim)
on Oct 22, 2012 at 03:32 UTC ( #1000255=perlquestion: print w/ replies, xml ) Need Help??
rpnoble419 has asked for the wisdom of the Perl Monks concerning the following question:

Has anyone tried out the Apache module for XSendFile with Perl? I installed it and it seams to be working (Apache started). I have tried a few attempts using the following Perl code to download a file:
print "X-Sendfile: C:\\Path\\to\\file\\video\\"; print "Content-Type:application/octet-stream; name=\"File-12.mp4\"\r\n +"; print "Content-Disposition: attachment; filename=\"File-12.mp4\"\r\n\n +"; print "\n\n"; exit;

I'm not getting the video to download. In Chrome, I not getting the correct header, and Apache is giving me a 404 error when I run the script. I'm trying to serve large video files after the user passes muster in my app. Here is the sample PHP code I based my Perl script on.

<?php ... if ($user->isLoggedIn()) { header("X-Sendfile: $path_to_somefile"); header("Content-Type: application/octet-stream"); header("Content-Disposition: attachment; filename=\"$somefile\""); exit; } ?> <h1>Permission denied</h1> <p>Login first!</p>

I tried to serve the file via Perl but the file is over 1.7GB and it hangs my server with an out of memory error.

Update: Sorry where are my manners. As always, Thank You for your time and help...

Update with Solution: I had two things wrong. The first was my Apache Config. The config must look like this in-order to work:


# enable xsendfile XSendFile On XSendFilePath C:/Path/To/Files/

The actual XsendFile On can be located in a vhost block as well, but the XSendFilePath must be located in the httpd.conf file.

My Perl header was in the wrong order. It needs to look like this:

my $file = "C:/Patch to file/FiletoSend.mp4"; my $filesize = -s $file; print "Content-Type:video/mp4\n"; # remove comment below to make file download. Add the comment to strea +m data to player #print "Content-Disposition: attachment; filename=\"FiletoSend.mp4\"\n +"; print "Content-Length: $filesize\n"; print "X-Sendfile: $file"; print "\n\n"; exit;

Now my system can stream the video after the user has been authenticated and I can create a one-time use URL.

Comment on Sending files with XSendFile
Select or Download Code
Re: Sending files with XSendFile
by Anonymous Monk on Oct 22, 2012 at 09:04 UTC
    Why do you sometimes print \r\n at the end, but sometimes you dont?
Re: Sending files with XSendFile
by zentara (Archbishop) on Oct 22, 2012 at 10:23 UTC
Re: Sending files with XSendFile
by daxim (Chaplain) on Oct 22, 2012 at 14:35 UTC
    Works for me. Show your configuration and real code. Describe in such detail that it is possible to reproduce the problem.
      If its working for you, then my Apache config may not be working.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1000255]
Approved by McDarren
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2014-07-26 17:52 GMT
Find Nodes?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:

    Results (178 votes), past polls