in reply to Re^3: printing file downloads
in thread printing file downloads

see that is my problem, I need to find out if I have to print a binary file or not, so is there a way to check if the file is a binary file or a text file? something like this:

my $filename = "file.gif"; my $filepath = "/path/to/"; if(-e "$filepath$filename") { my $_binCheck = is_file_binary("$filepath$filename"); if($_binCheck && $_binCheck == 1) { # print header for download and then open the file in binary mode +and print to window for the attachment } else { # print header for download and open the file normally and print t +o window for the attachment } } else { die "File does not exist: $filepath$filename"; }
That is what I need to do, is tell if the file is binary or text so I can print the correct way.

Anyone know the best way to do that?

Thanks,
Richard

Replies are listed 'Best First'.
Re^5: printing file downloads
by Marshall (Canon) on Dec 23, 2010 at 19:27 UTC
    I see that you understand that there is a -e "exists" file test operator, also there is:
    -T is a text file
    -B is a binary file (opposite of -T)

    If you are going to continue writing Perl, I strongly recommend that that you buy a copy of the language reference manual, Programming Perl by Larry Wall, et. al. In my 3rd edition, page 98 lists a whole slew of these file test operators so many that I can't count at a glance.

    if(-B "$filepath$filename"){...file is Binary...}
    However, as has been pointed out... ALL files can be sent as faithful copies of the binary bits.
Re^5: printing file downloads
by Anonymous Monk on Dec 23, 2010 at 19:31 UTC
    see that is my problem, I need to find out if I have to print a binary file or not

    I already covered this, there is absolutely no reason you need to find out, treat them all as binary, print the appropriate headers, and let the browser do its job

      sure enough, I tested it with a .gif and even though I did not set binmode it worked great... You were right.

      I did not understand that when you said it, I was working on no sleep in 19 hours so I was very tired... anyhow, it works great now.

      Thank you all for helping me resolve this.

      Richard