Alex Efros > CGI-Easy-SendFile > CGI::Easy::SendFile



Annotate this POD


View/Report Bugs
Module Version: v2.0.0   Source  


CGI::Easy::SendFile - send files from CGI to browser


This document describes CGI::Easy::SendFile version v2.0.0


    use CGI::Easy::SendFile qw( send_file );

    my $r = CGI::Easy::Request->new();
    my $h = CGI::Easy::Headers->new();

    my $data = send_file($r, $h, '/path/');
    print $h->compose();
    print ${$data};

    # -- send "file" generated in memory instead of real file
    my $dynamic_file = '…some binary data…';
    my $data = send_file($r, $h, \$dynamic_file);

    # -- simulate static image served by web server 
    #    (without "download file" dialog popup in browser)
    my $data = send_file($r, $h, 'avatar.png', {
            type    => 'image/png',
            cache   => 1,
            inline  => 1,


This module provide single function, which helps you prepare CGI reply for sending file to browser.


Nothing by default, but all documented functions can be explicitly imported.


send_file( $r, $h, $file, \%opt )

Prepare HTTP headers and content for CGI reply to send file.

    $r      CGI::Easy::Request object
    $h      CGI::Easy::Headers object
    $file   STRING (file name) or SCALARREF (file contents)
      {type}    STRING (default "application/x-download")
      {range}   BOOL (default TRUE if $file is STRING,
                              FALSE if $file is SCALARREF)
      {cache}   BOOL (default FALSE)
      {inline}  BOOL (default FALSE)

Custom value for 'Content-Type' header. These are equivalents:

    $data = send_file($r, $h, $file, {type=>'image/png'});

    $data = send_file($r, $h, $file);
    $h->{'Content-Type'} = 'image/png';

Enable/disable support for sending partial file contents, if requested (this is usually used by file downloader applications to fetch files faster using several simultaneous connections to download different file parts). You shouldn't enable this option for dynamic files generated by your CGI if contents of these files may differ for different CGI requests sent by same user to same url.

If your web server configured to gzip CGI replies, it will disable this feature. To make this feature working disable gzip in web server (usually by adding SetEnv no-gzip in .htaccess file).

When enabled and user requested partial contents will change 'Status' to '206 Partial Content'.


Enable/disable caching file contents.

HTTP header 'Expires' will be removed if {cache} is TRUE, or set to 'Sat, 01 Jan 2000 00:00:00 GMT' if {cache} is FALSE.

If {cache} is TRUE and $file is STRING will set 'Last-Modified' header; when browser use 'If-Modified-Since' and file doesn't changed will set 'Status' to '304 Not Modified' and return REF to empty string to avoid sending any needless data to browser.

You may want to add custom 'ETag' caching manually:

    $h->{ETag} = calc_my_ETag($file);
    if ($r->{ENV}{IF_NONE_MATCH} eq $h->{ETag}) {
        $h->{Status} = '304 Not Modified';
        $data = \q{};
    } else {
        $data = send_file($r, $h, $file, {cache=>1});
    print $h->compose(), ${$data};

Try to control how browser should handle sent file (this have sense only for file types which browser can just show instead of asking user where to save downloaded file on disk - like images).

If FALSE will set 'Content-Disposition' to 'attachment', this should force browser to save downloaded file instead of just showing it.

Return SCALARREF with (full/partial/empty) file contents which should be send as body of CGI reply.


Sending large files will use a lot of memory - this module doesn't use temporary files and keep everything in memory.


Bugs / Feature Requests

Please report any bugs or feature requests through the issue tracker at You will be notified automatically of any progress on your issue.

Source Code

This is open source software. The code repository is available for public review and contribution under the terms of the license. Feel free to fork the repository and submit pull requests.

    git clone



Alex Efros <>


This software is Copyright (c) 2009-2010 by Alex Efros <>.

This is free software, licensed under:

  The MIT (X11) License
syntax highlighting: