hello. i inquired elsewhere about how to offer mp3's for downloading by getting php to scan a folder, output the contents of that folder, and offer them for download.

here is a script which will do that just fine. it is a modification of a script offered in a comment at PHP.net: Glob - Manual. since the user comment which offered this script may not be there by the time you read this, i've left that user's comments intact at the top of my variation on the suggested use of the script.

HOWEVER, i'm STILL not satisfied with my own use of this thing-- i want the file to automatically prompt for download when it is clicked, instead of opening in QuckTime (on my system) and attempting to stream the file. i realize that this is an html issue, but i wanted to provide the script below, so i figured i'd ask in hopes that some kind lady or gent might offer his or her solution for an "auto-play" workaround.

before i installed Media Player Classic, Firefox WOULD automatically download when an mp3 was clicked, so...
in short-- how do i workaround the browser media-player plugin so i don't have to tell my users to "right click and 'save as' "? i hate that corny old phrase, and i don't want to be another who clutters my html code with that nonsense-- the user shouldn't have to think so much!

thanks for your help!

enjoy my variation on the PHP glob() function illustration below!!

<?php

/* alpharead version 3: This function returns an array containing the names of the files inside any given folder, excluding files that start with a '.', as well as the filenames listed in the '$killit' array. This array is sorted using the 'natural alphabetical' sorting manner. If no input is given to the function, it lists items in the script's interpreted folder. Version 3 fixes a MAJOR bug in version 2 which corrupted certain arrays with greater than 5 keys and one of the supposedly removed filenames.
written by Admiral at NuclearPixel.com */

function alpharead3($dir){
if(!$dir){$dir = '.';}
foreach(glob("$dir/*") as $item){$sort[]= end(explode('/',$item));}

$killit = array('index.html', 'index.php', 'thumbs.db', 'styles.css');
$killcounter = 0;
foreach($sort as $sorteditem){
foreach($killit as $killcheck){
if(strtolower($sorteditem) == strtolower($killcheck))
{unset($sort[$killcounter]);}
}$killcounter++;}
if($sort){natsort($sort);}
foreach($sort as $item){$return[]= $item;}

if(!$return){return array();}
return $return;
}

//some basic usage - list mp3 files for downloading

$filelist = "
			<table>
			<tr><th>Filename on Left- Click to Download at Right</th></tr>
			<tr><td><ol>";

$folder = 'files/mp3';
foreach(alpharead3($folder) as $item)
{
$filelist .= '<li>'.$item.' - <a href="'.$folder.'/'.$item.'" target=\"_parent\">download</a></li>';
}
$filelist .= "
			  </ol></td>
			  </tr>
			  </table>";



?>

    File handeling for download (i.e. how it downloads) is done on the client side. You, as a coder, can't force a download. Firefox 1.5 has the option to change the specific file handlers; however, IE doesn't (I think, i don't use, and I don't care to know). So if you wanted to force a download, you really can't unless you change your browser settings.

    You do have a couple options:
    1.) Offer the download in a zip format, and zip it on-the-fly with linux (if possible)
    2.) Make a note on the page that in order to download, you MUST "Right Click --> Save (Target) As..."

    ~Brett

      brett-- thanks! i like the zip idea (although, i don't know how to do that-- if you could provide a tip or tutorial, i'd be mucho grateful!)

      sorry-- i had edited my post as you were replying, i guess (re: "save as")

        Best way to do it, as far as I see, would be to use the passthru() function. Just use it with the zip command in linux (Dunno what that is, GZip for gz's, but there probably is one). It will return the binary data, which you then send to the client as a zip file.

        Correct me if I'm wrong, someone, never actually USED passthru().

          SargeZT wrote:

          use the passthru() function. Just use it with the zip command in linux

          can you please save me the few mins to several hours it might take for me to figure out how to do this? i'd really appreciate it. otherwise, i have no way of testing this until who knows when. thanks so much.

          i am using Linux cPanel X via Firefox on Windows XP

            oh, and regarding "can't force a download"... how then (as i've never tried this method) do online media galleries / libraries, etc. make a file download? surely napster.com doesn't try to stream the file when a member clicks to download, and i can't imagine they expect granny to know what "save as" means. i realize some of these media purchasing clubs have proprietary download managers, however-- and i don't have the skill or knowledge to write one of those-- and of course that's a little over the top for what i'm trying to do.

            is "save as" really our only option when it comes to this issue? i find that difficult to believe. hmph. (not difficult to believe you, but difficult to believe in general)
            😉

              Well, here's the way to dynamically generate a zip file, works well on my server.

              <?php
              header("Content-Type: application/octet-stream");
              header("Content-Disposition: attachment; filename=\"myfile.zip\"");
              $musicfile = escapeshellcmd($_GET['mp3']);
              exec("zip music $musicfile");
              passthru("cat music.zip");
              exec("rm music.zip")
              ?>

              However, I'd certainly reccomend adding in a caching system, or else your CPU usage will go up the wazoo.

              Secondly, you could add a text file in the zip file telling people where it was downloaded from by simply adding a second file in the zip command.

              As for your 'can't for a download problem', I have no clue.

                hey, SargeZT... thanks SO MUCH. very awesome of you, and i'll bet many others will find it to be useful as well, so you do indeed rock!

                where might i find more info on this caching issue? i'm not sure what's goign on there... why is it an issue, etc? what does d/l have to do w/ CPU usage in this situation versus downloading any other file for example?

                  The issue in this case is the fact that you're zipping up a file every time someone goes to download it. Zipping requires CPU time, quite a bit of it, and it would work better if you only zipped the file once, when it was requested the first time.

                  What caching would do is make the zip file, move it somewhere, and the next time someone requests the file, just send that to them instead of zipping it up again. Pretty easy stuff if you have a database available.

                  Oh, and if you need to know how to do it, I'd be more then glad to show you how.

                    well-- yes, i do need to know how to do it. hehe.

                    i'm thinking... can't i zip on the way up instead of on the way down then?

                    hmm... i think i'm gonna make a temp directory, so you can see precisely what i'm up to here. can't show the real thing as it is a private location. ... stay tuned...

                      That's completley reasonable as well. In fact, far more reasonable then the caching idea, now that I think about it. I'm so used to dynamic content 🙁

                      If you just zip the files on the way up, then you would indeed accomplish the same thing. Are the files uploaded through PHP?

                        Indeed, it should be an easy thing to do if you have an upload script. Any other questions?

                          Well, what a few short strokes on gMail give is this:

                          Force Download and cache Media Files Script

                          A PHP Manual User Note:

                          A mime-type-independent forced download can also be conducted by using:

                          <?
                          (...)
                          header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // some day in the past
                          header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
                          header("Content-type: application/x-download");
                          header("Content-Disposition: attachment; filename={$new_name}");
                          header("Content-Transfer-Encoding: binary");
                          ?>

                          Cheers,

                          Peavey

                          Perhaps that would help you if you don't want to zip it down....

                          ~Brett

                            brett -- that's AWESOME! thanks!!!

                            hey, what does this mean-- is there something on gMail that i don't know about, or is this from your personal collection? i'm confused by that.

                            bpat1434 wrote:

                            a few short strokes on gMai

                            SargeZT wrote:

                            Any other questions?

                            well-- i don't think so-- think we've pretty much covered it! 😉
                            however, i would be delighted if you have any feedback about my mp3 upload technique via this script... look for a link to my code soon as i can get it there in a sec.. (i assume i'd need to stick the zip thing here if i choose to do that) http://adesigninteractive.com/phpbuilder/mp3upload.php

                            but, zipping doesn't really do much for mp3's, does it-- in terms of file size? assuming i should choose to go w/ brett's option?

                            EDIT:
                            i must admit, however-- there's a certain coolness to the zip thing that i think i want to try-- for future use as well-- it will be a good thing to be acquainted with.

                              Zipping doesn't do anything for filesize, no. If anything, it shaves off like 100 kb (if that). Remember: mp3 is already compressed as much as it can be. So zipping it would just be a way to force a download.

                              ATS: I wrote "gMai" and I meant google. I was checking mail, and forgot to type the correct word. I'm not that geeky to store PHP code from the manual in my gmail account... although a novel idea.... A constant searchable PHP MANUAL!!! Woo Hoo!!! wait wait wait.... they've already done that.. CRAP!!

                              ~Brett

                                For the average MP3, you'd only see a 2-3% reduction in bandwidth. I'd personally go with Brett's solution, as it doesn't use as much CPU time at upload. Either way would work, and zipping does add extra complexity.

                                Edit: Sorry brett, you posted yours while I was typing.

                                  It's okay, as long as ATS is satisified with the respone(s) he has received, it doesn't matter who helped, or whatever, just that he got it answered.

                                  ~Brett

                                    SargeZT, darn it-- i'm afraid i've discouraged you from teaching me something VERY valuable here-- do you mind proceeding w/ the zip - upload bit, or a brief explanation of how to reverse what you've shown me already? i found this to be very interesting-- even if not practical for THIS purpose.

                                    thanks so much to both of you.

                                    brett-- now i understand what you mean-- and storing code WOULD be good-- cause you'd have it limited to what you want-- although, yes, geeky. hehe. i figured you must have meant google after i thought about it-- but, it would have taken me a month to find that probably-- i reckon you knew a bit better what to scan for that I would have. thanks again

                                      bpat1434 wrote:

                                      It's okay, as long as ATS is satisified

                                      😃

                                      yes, very.

                                      and, in my opinion, this has become one very valuable thread-- for two completely differet, excellent code resources!!

                                      EDIT:
                                      hey, Brett-- was there any mention of what is the purpose of the Date expiration bit? (just examining the code now)
                                      do you still have the URL from where you got that?