jrsweets Posted March 23, 2004 Posted March 23, 2004 How do you make it so when a user clicks on a link to a file it will pop up the box like the screen shot that I attached? When a user clicks on my file it either streams it, or opens the file. It happens when I wanted to post a small wav or mp3 file. The file is streamed, but I want the user to have the option to save the file to. Quote
Wilexa Posted March 23, 2004 Posted March 23, 2004 Sorry, no answer here. But I was just about to ask the same question! So there are at least two TCHers that would love to know how! I was thinking that maybe the MIME type would have to be changed? ...dave Quote
hindixp Posted March 23, 2004 Posted March 23, 2004 You can use a file download script or even a Antileech one. See ya, manu Quote
kaseytraeger Posted March 23, 2004 Posted March 23, 2004 The only way I know of to do this is to encode it in a plain anchor link (which you're probably already doing). In fact, I have a movie on one of my web pages right now. There is a link on another web page (on another web site, too) that directs the user to the movie. That link is coded like this: ><a href="http://www.****/movie.wmv">Movie</a> But I just tested this link, and the movie opens straight away and doesn't offer the option to "open" or "save." I imagine this is because I've already got my computer's preferences set up to open Windows Media Player when it encounters various multimedia files. You could offer a small piece of advice to your users by telling them that they can save the movie (or music file, whatever) by right-clicking on the link and selecting "Save Target As." Other than that, I'm not sure what else to do. This is the first time I've paid any attention this behavior and even cared whether someone had the option to save or open a movie file directly from my web site. I hope something I've mentioned has been helpful! Quote
DarqFlare Posted March 23, 2004 Posted March 23, 2004 I've discovered how to do it in PHP... If you'd like, I can post it. Quote
kaseytraeger Posted March 23, 2004 Posted March 23, 2004 I just thought of something else you could do. Instead of offering a download of the movie straight away, why not save a few bytes of bandwidth and Zip the file? Then code your link like this: ><a href="www.****/movie.zip">The Movie</a> I think links to Zip files always cause the "open/save" pop-up window to appear. Quote
kaseytraeger Posted March 23, 2004 Posted March 23, 2004 I've discovered how to do it in PHP... If you'd like, I can post it. That would be great, Robert! I'd like to use it on the movie I currently have on my web site! Please ... post away!!! Quote
!!blue Posted March 23, 2004 Posted March 23, 2004 If you want to be able to do this on your computer, on a PC you'd have to change your File Types. In Windows Explorer, go to Tools > Options... In the dialog box, click on the "File Types" tab Take a breath as it loads all the diff file types Scroll down to "MP3" Click on it (highlight it) and then click on "Advanced..." You'll get a new window that lists all the actions associated with this file type. Near the bottom is a checkbox "Confirm open after download" This makes sure the browser asks you if you want to open the file or save it. Click "OK", Click "OK" and finally... click "OK" FYI: this is where you can change what programs open what files. For example, I have all my *.html files specifically edit with dreamweaver. But don't go crazy and start changing everything around ok? Cuz I made a mistake one time and took me forever to figure out what I did. hope that helps someone out there! Now if I could only figure out how to do this on a Mac....Yuck! later, !!blue Quote
DarqFlare Posted March 23, 2004 Posted March 23, 2004 This is a generalization of what you'd need to do, in PHP: ><? ini_set('session.cache_limiter', ''); header('Expires: Thu, 19 Nov 1981 08:52:00 GMT'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: no-cache'); header("Content-Disposition: disposition-type=attachment; filename=XXXa"); readfile("XXXb"); ?> This is a PHP script that you would link to in a page, much like this: ><a href="script.php">Download MP3</a> This is how I've got things working, more or less. Let me know if it works for you. Replace 'XXXa' with the suggested file save name, and the 'XXXb' with the local (I do believe) path to the file you want them to download. Quote
kaseytraeger Posted March 24, 2004 Posted March 24, 2004 Robert, I just realized that the movie and the link to the movie are not on the same domain. From what I understand of the script you wrote, it's expecting that the file is sitting on the same domain as the link. Should I just modify the code to explicitly state the location of the movie, as in: >readfile("http://www.domain.com/movie.wmv"); Instead of >readfile("XXXb"); Thanks for your help!!! Quote
DarqFlare Posted March 25, 2004 Posted March 25, 2004 I don't think PHP can read a file over the internet, but I haven't tried it. It's worth a shot. Quote
kaseytraeger Posted March 25, 2004 Posted March 25, 2004 Folks, I have very good news!!!!! Robert's script works perfectly whether or not the movie is sitting in the same domain as the link to the movie. I just tried it out, and here's how you code it to make it work. Code for the PHP script (stored on your domain): ><?php ini_set('session.cache_limiter', ''); header('Expires: Thu, 19 Nov 1981 08:52:00 GMT'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: no-cache'); header("Content-Disposition: disposition-type=attachment; filename=movie.wmv"); readfile("http://www.remotedomain.com/movie.wmv"); <!-- the movie,mp3,mpeg file can be sitting on a remote domain and you just call to it. --> ?> Code for the calling link: ><html> <head> <title>Testing Movie Download</title> </head> <body> <a href="/path/to/script.php">Download Movie</a> <!-- Where script.php is stored on your domain --> </body> </html> Thank you, Robert, for coming up with such a clever way to force a download box! Quote
snipe Posted March 25, 2004 Posted March 25, 2004 I don't think PHP can read a file over the internet, but I haven't tried it. It's worth a shot. Actually it can, assuming you have those features enabled on the server. fopen() can open local files *or* urls - http://us2.php.net/manual/en/function.fopen.php and PHP has a bunch of ftp functions built in as well: http://us2.php.net/manual/en/ref.ftp.php Quote
DarqFlare Posted March 25, 2004 Posted March 25, 2004 Good to hear that readfile works across domains... I was hoping it would. There are really a variety of ways to force a download box, but I like to use a PHP script to do it because a PHP script can employ a variety of controls, such as checking for logged in users, etc. Quote
jrsweets Posted March 26, 2004 Author Posted March 26, 2004 I get this error. Both files are saved at the root directory. Parse error: parse error in /home/jeffrus/public_html/script.php on line 10 ------------------------------------ Test.htm <html> <head> <title>Testing MP3 Download</title> </head> <body> <a href="script.php">Download Movie</a> <!-- Where script.php is stored on your domain --> </body> </html> ------------------------------------ script.php <?php ini_set('session.cache_limiter', ''); header('Expires: Thu, 19 Nov 1981 08:52:00 GMT'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: no-cache'); header("Content-Disposition: disposition-type=attachment; filename=ARTIE-FCC-TNT-SONG.mp3"); readfile("http://jeffrusso.net/av/ARTIE-FCC-TNT-SONG.mp3"); <!-- the movie,mp3,mpeg file can be sitting on a remote domain and you just call to it. --> ?> Quote
jrsweets Posted March 26, 2004 Author Posted March 26, 2004 I got it working. It didn't like the comment file. Thanks. Another problem solved. I do have a question. What if I have say 5 files I want to download? Do I have to write 5 different scripts or is there a way to modify this to include more files? Quote
kaseytraeger Posted March 26, 2004 Posted March 26, 2004 jrsweets, Was it the comment line I inserted in the php file that screwed it up? I must apologize. I used the wrong commenting tags. In PHP, you comment with //, but in XHTML, you comment with <!-- and -->. Guess I got the two mixed up. That's what happens when you've got a more than a half dozen programming languages tucked away in your brain. Sometimes the wires get crossed and you start trying to speak VHDL to XHTML or PHP to C. Anyway, glad to hear that it's working now. If it were me and I wanted users to be able to download more than one file at a time, I'd zip 'em all and use just one download. Alternately, you can give them different options. I'll work on a piece of code for you and then post it here. Give me a bit of time. I'll be back... Quote
kaseytraeger Posted March 26, 2004 Posted March 26, 2004 OK, here's what I've come up with. The HTML is coded as follows. ><html> <head> <title>Testing File Download</title> </head> <body> <!-- Where script.php is stored on your domain --> <ul> <li><a href="/path/to/script.php?filenum=0">Download File1</a></li> <li><a href="/path/to/script.php?filenum=1">Download File2</a></li> <li><a href="/path/to/script.php?filenum=2">Download File3</a></li> <li><a href="/path/to/script.php?filenum=3">Download File4</a></li> <li><a href="/path/to/script.php?filenum=4">Download File5</a></li> <li><a href="/path/to/script.php?filenum=5">Download All 5 Files</a></li> </ul> </body> </html> The PHP file called "script.php" could be coded like this: ><?php ini_set('session.cache_limiter', ''); header('Expires: Thu, 19 Nov 1981 08:52:00 GMT'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: no-cache'); $files = array('nameoffile1.ext', 'nameoffile2.ext', 'nameoffile3.ext', 'nameoffile4.ext', 'nameoffile5.ext', 'nameoffile6.ext'); $filename=$files[$filenum]; header("Content-Disposition: disposition-type=attachment; filename=".$filename); readfile("http://www.remotedomain.com/path/to/file/".$filename); ?> Here is how it is supposed to work. Say you have five different files, all of different types (e.g., the first is a wmv movie file, the second a music mp3, the third a Microsoft Word document, the fourth an Adobe PDF document, and the fifth is an avi movie). Code your links as shown in the html list (I chose to use a list because that seems a logical way of organizing links). The variable "filenum" will be used as an index into a PHP array. For each additional file you want to add, just increase the filenum by one for each successive file. Inside the PHP file, the array $files should contain a list of the names of each file name. For example, using the order of files given two paragraphs earlier, I would put those files into the array like this. Also note that I have included an extra zip file at the end that handles the download of all five files. >$files = array('moviefile.wmv', 'musicfile.mp3', 'mswordfile.doc', 'adobepdffile.pdf', 'moviefile.avi', 'allfivefiles.zip'); The next line >$filename=$files[$filenum]; uses the index number assigned to the file in the HTML code to pull the proper file from the array. The next two lines of code are identical to the previous version of this program, except that instead of explicitely stating the name of the file to be downloaded, the PHP script will use the name of the file based on which link was clicked, because each link is associated with a different file name. This script assumes that all the files are sitting in the same directory on the same domain. Now, I haven't actually tested this, so I don't know if it will work. You'll have to play around with it. I am here to help, however. So if you have any questions, don't hesitate to give a shout-out. Also, I just read the PHP documentation on the Content-Disposition option for headers. There is no indication that more than one file can be downloaded per download box. That being the case, I would just zip all the files and offer them an option to download all five, as shown in the HTML. Again, don't forget that I'm still here to help. Good luck! Quote
jrsweets Posted April 2, 2004 Author Posted April 2, 2004 Sorry I'm been away from the computer for a couple of days. I will try it this weekend. Thanks for all the help. Thats what I was looking for. I didn't mean to include all the files in one dialog box. Thanks again. Quote
bzzzz Posted April 6, 2004 Posted April 6, 2004 hello all. this is what i'd like to do as well...however, i don't want to manually make all of the links...i'd like them to be automatically generated. the idea is for people to have to download the file, as to eliminate any confusion for the newbies. i am doing the 'ftp' part as per this post: auto generated ftp list and poking around the web i found this, which may be helpful, but my php skills are next to zero. php force download any help would be appreciated. thanks, ...glenn. Quote
voilsb Posted April 7, 2004 Posted April 7, 2004 I modified the code provided above to auto-generate the links. It's two files. Here they are: links.php ><?php $webdir="/home/username/public_html"; $targetdir="/downloads"; $downloaddir=$webdir . $targetdir; function hsize($file) { // gets the filesize of the files and formats them in a human-readable format global $downloaddir; $filepath = "$downloaddir/$file"; // Setup some common file size measurements. $kb = 1024; // Kilobyte $mb = 1048576; // Megabyte // Get the file size in bytes. $size = filesize($filepath); /* If it's less than a kb we just return the size, otherwise we keep going until the size is in the appropriate measurement range. */ if($size < $kb) { return $size." bytes"; } else if($size < $mb) { return round($size/$kb,2)." Kb"; } else { return round($size/$mb,2)." Mb"; } } function ls() // this function prints a directory list with HTML links { global $downloaddir, $targetdir; $dir_list = array(); $dir = opendir($downloaddir); while (false !== ($file = readdir($dir))) // read the files in the directory to an array { if ( ! ereg('^\.', $file) ) // don't include files that start with '.' in the list $dir_list[] = $file; } closedir($dir); natcasesort($dir_list); // sort the directory alphabetically; case insensitive. use natsort() or asort() if you want case-sensitive sorting reset($dir_list); // read the filenames and sizes from the array and generate the HTML links while (list($key, $val) = each($dir_list)) { $file_size = hsize($val); // get the file size for each file as it's read from the dir_list array // use this one if using ModRewrite echo "<tr><td><a href='/download/$val'>$val</a></td><td> </td><td>$file_size</td></tr>"; // uncomment this if you don't have/use ModRewrite // echo "<tr><td><a href='get.php?val=$val'>$val</a></td><td> </td><td>$file_size</td></tr>"; } } // generate the actual webpage: echo "<html><body><table>"; echo "<tr><td><b>Filename</b></td><td> </td><td><b>File Size</b></td></tr>"; ls(); // call the directory list function and generate the download list // close up the webpage echo "</table></body></html>"; ?> get.php ><?php $webdir="/home/username/public_html"; $targetdir="/downloads"; $uploaddir=$webdir . $targetdir; if (file_exists("$uploaddir/$val")) //check if the file exists, provide a 404 error if it doesn't { header("Content-Disposition: disposition-type=attachment; filename=$val"); readfile("$uploaddir/$val"); } else { echo 'User from '.$_SERVER["REMOTE_ADDR"].' encountered an <h1>error 404: File Not Found</h1> at ****** while attempting to access /download/'.$val.' with '.$_SERVER['HTTP_USER_AGENT']; } ?> This code uses the following rewrite rules for your .htaccess file: ><IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^download/([\ -z]+) get.php?val=$1 </IfModule> Quote
DarqFlare Posted April 7, 2004 Posted April 7, 2004 Looking good! I've got my mod rewrite setup in a smiliar fashion... Quote
bzzzz Posted April 9, 2004 Posted April 9, 2004 hello all. having a few problems on this end and i am sure it is stupid stuff that i am not seeing. first: where should get.php and links.php live? in which directory? can the path to them be set? i have them in the public_html directory and also the testing directory... and the webdir: $webdir="home/MYUSERNAME/public_html"; is the above be correct? how do i know the path to my site? i seached the FAQ and the welcome letter to no avail. $targetdir="/testing"; this is a bit confusing to me...i'd call it a source directory, since it i want to download, it could just be a syntax thing i guess. my test is here: http://mascotopia.com/testing/ my htaccess file is the same as listed...nothing more nothing less...and i changed both the testing htaccess file as well as the public_html access file. any pointers would be great! thanks. Quote
voilsb Posted April 9, 2004 Posted April 9, 2004 Basically, I have get.php and links.php in the root directory. $webdir="home/MYUSERNAME/public_html"; won't work ... you need the leading slash. It should be "/home/MYUSERNAME/public_html" Yes, the $targetdir vs $sourcedir thing is syntax ... feel free to rename it. And in my particular case, I have it set where users cannot view the actual folder. The files are in public_html/downloads (though they don't really even have to be in public_html .. you can change the source to fit) and the URLs are pointing to download/FILENAME. So you would access myhostname.com/links.php and get a list of myhostname.com/downloads, which you can't view with a web browser (.htacces contains 'deny from all'). links.php generates URLs pointing to myhostname.com/download/FILENAME (a directory that doesn't really exist) and the public_html .htaccess file translates myhostname.com/download/FILENAME into myhostname.com/get.php?val=FILENAME and spits out the file. Of course, you can change all that if you wanted by changing the variables. $webdir should remain "/home/MYUSERNAME/public_html" but $targetdir can be whatever you want it to be. Or, you can just outright replace $downloaddir with wherever you want it to be, so long as it's readable by php. You will have to likewise adjust your RewriteRules, though. Quote
section31 Posted April 26, 2004 Posted April 26, 2004 Interesting..I wrote my own download script for my site a long time ago (1 year ago) but i'm using just the header php function. That is the only one I actually understand. header -- Send a raw HTTP header I see you people using the readfile and fopen function...I read up on them on php.net and I don't quite understand how they open files across domains. Says something about a url wrapper and I'm unclear on what that is. Does anyone know what the wrappers do exactly, it seems both fopen and readfile use them. Does this read them through the server and then send them to the client...if that is the case, i'll stick to a header redirection so it won't waste unnecessary resources. Thanks for any help, Dave Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.