Creating m3u list from opendir listing

S

Seansan

Hi there,

Does anyone know why the following code doenst force a download of a m3u
list (winamp playlist) when called directly? Am I forgetting something?

regards, Seansan

my $mp3_dir = '/var/www/html/dl';
my $url_root = 'http://www.site.com/dl/';
my $body;

opendir(D, $mp3_dir);
my @f = readdir(D);
closedir(D);
foreach my $file (@f) {
next if ($file eq '.' || $file eq '..');
next if (-d $file);
if ($file =~ /mp3$/) {
$body .= $url_root . $file . "\n";
}
}

print "Content-type: application/octet-stream\n";
print "Content-Disposition: attachment; filename=mymusic.m3u\n";
print "Content-Length: ", length $body, "\n\n",
print $body;
 
T

Tad McClellan

Seansan said:
Does anyone know why the following code doenst force a download of a m3u
list (winamp playlist) when called directly?


Yes, anyone who reads the documentation for the functions that
they use knows why your program is failing.

Am I forgetting something?


You are forgetting to read the documentation for the functions
that you use.

opendir(D, $mp3_dir);


You should test to see if you actually got what you asked for:

opendir(D, $mp3_dir) or die "could not open '$mp3_dir' dir $!";

my @f = readdir(D);
next if (-d $file);


perldoc -f readdir

...

If you're planning to filetest the return values out of a
"readdir", you'd better prepend the directory in question.
Otherwise, because we didn't "chdir" there, it would have been
testing the wrong file.
 
A

axel

Seansan said:
Does anyone know why the following code doenst force a download of a m3u
list (winamp playlist) when called directly? Am I forgetting something?

I think there is a typo...
print "Content-type: application/octet-stream\n";
print "Content-Disposition: attachment; filename=mymusic.m3u\n";
print "Content-Length: ", length $body, "\n\n",
^^^^
should be ;
print $body;


Axel
 
S

Sherm Pendley

Seansan said:
Does anyone know why the following code doenst force a download of a m3u
list (winamp playlist) when called directly? Am I forgetting something?

Others have already pointed out the errors in your Perl - it's obviously
not your real code. It won't even compile.

Anyway... Your question is about HTTP headers and web browsers. It
really has nothing to do with Perl. You're asking *what* to print, not
how to print, so the fact that you happen to be printing with Perl is
incidental.

For the sort of question you're asking, you'd get more and better advice
by posting to a group that's focused on CGI programming for the WWW -
for instance, comp.infosystems.www.authoring.cgi might be a good choice.

sherm--
 
S

Sherm Pendley

To be fair... it does compile.

Okay, I thought the typo you pointed out earlier would have prevented
it. Even so, it does cause an error in the output. With the misplaced
comma, it triggers a "500 Server Error". With that corrected, my default
browser (Safari) downloads the file and saves it with the suggested
name. FireFox pops up a "do you want to open or save this file?" dialog.

Clearly, the question concerns the state of browser support for these
HTTP headers. That's a question that has nothing to do with the language
you happen to be using to print them.

I'm not just pointing this out to be picky, either - like I said, I
believe the OP will get more and better advice from a group where just
this sort of question is the main focus.

sherm--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top