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

Perl vs. PHP in streaming a file

by krkeegan (Novice)
on Nov 24, 2004 at 17:42 UTC ( #410192=perlquestion: print w/replies, xml ) Need Help??

krkeegan has asked for the wisdom of the Perl Monks concerning the following question:

I have been tinkering with using perl or php to serve files to my TIVO.

When I use perl the streamed music file skips badly, and no matter how I alter the amount read before it prints I still get really bad skipping. But if I use explorer to download the same file thru the script I get hugh kbps. Here is a portion of my script:

This is a file that was opened with IO:File
elsif( ref $data eq 'IO::File' ) { my $block; while( $data->read($block, 1024) ) { print $block; } # Close file undef $data;
So I went and tried a php version that some else had created and it worked perfectly. The php version used fopen and fpassthru.

My question is, I don't think this is a speed problem with perl since I can download the file thru the script superfast. But WHY IS PHP WORKING SO MUCH BETTER AT THIS TASK? And is there a better perl method to read and send the file?

Thanks for your help,


2004-11-25 Janitored by Arunbear - added code tags, as per Monastery guidelines

Replies are listed 'Best First'.
Re: Perl vs. PHP in streaming a file
by ikegami (Patriarch) on Nov 24, 2004 at 18:45 UTC

    I was going to try different things, but a file being printed by the code you provided downloaded at 280KBs!!! (That's 1MB in 3.6 seconds!) I don't want to get into trouble with my employer or my web host, so I'm not going to do further testing.

    Things I was going to try:

    1. Unbuffered (sysread and syswrite instead of read and print).
    2. Autoflushed STDOUT.
    3. Using the NPH feature of CGI.
    4. Combination of 1 and 3.
    5. Combination of 2 and 3.

    Actual code used:

    #!/usr/bin/perl use strict; use warnings; print("Content-Type: application/x-octect-stream\n"); print("\n"); use IO::File (); my $data = IO::File->new('< file') or die("Can't open data file: $!$/"); binmode($data); my $block = ''; while ($data->read($block, 1024)) { print $block; }
Re: Perl vs. PHP in streaming a file
by exussum0 (Vicar) on Nov 24, 2004 at 18:13 UTC
    Have you tried to bump up the block size? It will be a trade off of memory to speed. php excels because it, if I understand right, can use a shortcut, like linux has, to pipe a file directly to a tcp/ip socket.

    I think that's right.

    Then B.I. said, "Hov' remind yourself nobody built like you, you designed yourself"

Re: Perl vs. PHP in streaming a file
by howie (Sexton) on Nov 24, 2004 at 18:54 UTC
    I don't understand why you are using perl at all... what is it you are doing that Apache can't do? Taking a static file, and feeding it to a network socket is exactly what httpd does all day and night, and presumably is optimised pretty well to do... this isn't meant to sound negative - I'm really curious what you are gaining. I can certainly stream music that way, and mplayer has worked with divx too, although I do that less often.
      Thanks for your help so far. To the response of 280kbps I get the same speed out of my machine too with the perl script but for some reason the Tivo box really lags when I use perl, but not with php. My assumption is that the data is not reaching it fast enough, since this is a streaming file, but it could be another problem too. I am just trying to figure out what is different with PHP and perl in this respect.

      And to answer the question why I don't use apache. I actually am, TiVo and the homemedia option always use an alias http://server/TiVoConnect so in order to make it function right I have to use a script to be able to determine what is a query or what is a data transmission.

      Thanks again.
        TiVo and the homemedia option always use an alias http://server/TiVoConnect so in order to make it function right I have to use a script to be able to determine what is a query or what is a data transmission.

        According to a document I found on TiVo's website, the TiVo uses two different request formats:

        Meta-data requests
        File requests

        These are different enough that you could use Apache's mod_rewrite to direct file requests directly to the file, and then you would need neither PHP nor Perl to serve them.

        If your getting that high throughput I'd venture a guess that speed is not your issue. Is your script running on windows? If you are running on Windows you will almost cretanly need binmode(filehandle). I didn't see it in the code you posted. With out it Perl will open the file assuming that it is text and it will likely corrupt it as it's read.
      I don't know what is going on in this case (and I don't know TIVO at all) but in the past I have worked on www sites that charge for media files (ie images and videos). I have used CGI so I could check that the user is logged in and allowed to download the file.

      I don't really know why krkeegan is having trouble though if he is able to downlaod at 280kb/second unless that is not fast enough for the video stream. How fast does it download using PHP's fpassthrough function?

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2022-09-28 15:55 GMT
Find Nodes?
    Voting Booth?
    I prefer my indexes to start at:

    Results (124 votes). Check out past polls.