473,811 Members | 2,749 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Forcing the browser to download a music file

8 New Member
i dont know php & i want a script that when a user click on a mp3 hyperlink, the mp3 file start downloading instead of start playing in media player.
i got a sxript from internet search.
i am using the following code
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.           $filename = "path/to/file/filname.ext";
  3.           if(!file_exists($filename)) {
  4.               die("File does not exist!");
  5.           }
  6.           header("Pragma: public");
  7.           header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  8.           header("Cache-Control: private",false);
  9.           header("Content-Disposition: attachment;   filename=\"".basename($filename)."\";" );
  10.           header("Content-Transfer-Encoding: binary");
  11.           header("Content-Type: PHP Generated Data");
  12.           header("Content-Length: ".filesize($filename));
  13.           readfile("$filename");
  14.           exit();
  15. ?>
  16.  
i have saved this code with name download.php
every time code give error message that "File does not exist!"
i have uploaded the download.php file in the same directory where the mp3 file is uploaded and i am using the folliwing hyper link code in the html file
Expand|Select|Wrap|Line Numbers
  1. <a target="_blank" href="download.php?location=ABC.mp3">downlaod ABC</a>
index.html, download.php and ABC.mp3 are in the same directory
in IE-7 it start downloading file with the name i specified in the code, but with size 27bytes, abd after opening this file in notepad i get the error "File does not exist!". BUT in Mozilla firefox it give same erroe message in a new window.

can anyone tell me what is the problem???????
Thanx
Sep 15 '07
14 4609
Hackles
18 New Member
Just an amendment to the last post: As far as I know of, it is impossible to set the FilesMatch rule for a specific directory (I've tried .htaccess, nesting within a Directory rule, even adding a path to the regex for the FilesMatch rule, but it doesn't seem to work). I guess that'd be a slight downside, not being able to control which directory falls under direct download control.
An alternative (that achieves the same result) is:
Expand|Select|Wrap|Line Numbers
  1. AddType application/octet-stream .mp3
  2.  
The benefit of the above handler (which goes in httpd.conf) is that you don't need mod_headers enabled.
Note that any changes to httpd.conf will require a server restart.
Hope this helps...
Have fun!
Sep 17 '07 #11
Irfan12
8 New Member
Thanks for reply,
but as i said in the start that i dont know even PHP, than how can i understand what is to change and where to change/add your give Code.
i am using linux webserver, kindly tell me the simplest way so that there may be a resume support in downloading,
In adddation to my last post, i want to tell that Mozilla firefox's download box have the resume support, but download accelerator dont have resume support.

thakyou
Sep 17 '07 #12
Irfan12
8 New Member
Just an amendment to the last post: As far as I know of, it is impossible to set the FilesMatch rule for a specific directory (I've tried .htaccess, nesting within a Directory rule, even adding a path to the regex for the FilesMatch rule, but it doesn't seem to work). I guess that'd be a slight downside, not being able to control which directory falls under direct download control.
An alternative (that achieves the same result) is:
Expand|Select|Wrap|Line Numbers
  1. AddType application/octet-stream .mp3
  2.  
The benefit of the above handler (which goes in httpd.conf) is that you don't need mod_headers enabled.
Note that any changes to httpd.conf will require a server restart.
Hope this helps...
Have fun!


Thanks for your code,
i am using download accelerator to do download my files but it gives error message in start of downloading that "resume not supported". means if user lost his connection than it is not possible to start/resume download from the broken state. Means that if a user is downloading 3 mb file, and after 2 mb of download if user lost internet connection, or due to power failure, than the user will lost 2 mb, and user must start downlaoding from the beginning.
Actualy, when i use download.php for forced download than there is no resume supprt, but when i use normal downloading (means to right click on link and select "save target as...") than there is a resume support, but by using download.php (above given code) there is no resume support.
Kindly tell me is there any problem in the above code, or what should i do?????
Thankyou
Sep 21 '07 #13
whiteyoh
13 New Member
hi all,

i have been following this thread with much interest, and it works fine, except for one thing.......

when downloading it prefixes the file with .html. so for example, a fill downloads as mymusic.mp3.htm l

how can i get it to scrap the HTML bit?
Jul 2 '08 #14
pbmods
5,821 Recognized Expert Expert
Heya, whiteyoh.

Check out this article (http://www.phpit.net/code/force-download/).
Jul 3 '08 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

3
1772
by: D. Alvarado | last post by:
Hello, I have a page that generates a text file (but writes the file with the ".csv" extension) and then I redirect the user so he can save the file header("Location: $my_csv_file"); However, the file appears as text in the browser and I would prefer that the gray "Download this file" box pop up. How could I pull this off, or is this really more dependent on client browser settings?
3
8622
by: Julie | last post by:
I have an html file where I display name,address,zip. It is one per line so it is basicall a list of addresses. But I would like ie6 to force the download prompt of this long list of names instead of displaying it to their browsers. This way they can save the list to a file. I know I can set it up as a txt file and let them download it that way, but right now this file is generated from a source program which I have no control over...
1
1211
by: Do | last post by:
Hi, My issue is twofold. 1) How do I invoke a "save file" action when a user clicks a button. In classic ASP, I used to response.redirect to file path. But I want to keep the user on the same page they clicked "Download" button. 2) How do I force to prompt for a save file when the user clicks the button rather than try to open and read the file in the browser, which is the
4
3404
by: randmCP | last post by:
Hi and thanks in advanced for you help. I am working on a file-distribution asp.net application. Users click on a custom grid column to download files stored on a SQL DB. The client does not want to open the file in the browser, so how can I either: 1. Display the Save Target as dialog box to download the a file when the file is in memory using the WebFileResponse class. 2. or simulate the right click (Save Target) from a hyperlink to a...
4
1538
by: teeBull | last post by:
Hi all, We'd like to take advantage of code we already have for transforming XML into HTML (using XSLT) for our users to save the HTML as an MS Word document locally. I've dug around and found the following code to include in the code behind: Response.ContentType = "application/vnd.ms-word" Response.AddHeader("Content-Disposition", "inline;filename=someFile.doc")
12
2924
by: devospice | last post by:
Hi, I'm trying to create a download counter for individual files on a web site and I'm not sure how to do this. Right now I'm using Webalizer to just read the log files and see how many times the files I'm interested in were downloaded. The problem is Webalizer breaks it up by month and I want a running total. I'd also like to see the counts without having to log in to Webalizer in the first place. Not that logging in or adding the...
2
3929
by: FlashForumKB | last post by:
Here is a chance for you to make my developers look bad. I have hired these guys to development my website which, in part, has music demos available to my users. These demos must include the entire piece with a spoiler in the background so users can not record them freely. The files must be secure! My original request was to have them build an application that merged the two files (spoiler and original music) into a single mp3 file and have...
7
2654
by: lawrence k | last post by:
I've got a music studio for a client. Their whole studio is run with Macintosh computers. Macintosh computers allow file names to have open white spaces, such as "animal hospital.mp3". I have a download script, so customers on the website can download MP3s to their harddrive (rather than merely listen to it in their browsers):
3
1844
by: Don | last post by:
Is it possible to create a link which will cause either A) the server to serve a fresh copy of a file or B) the browser to "refresh" the copy of the file. Doing it via a link is the only possibility that I think would be viable for our situation, as changing HTTP headers isn't really feasible/desired right now (think 'expires' or 'no-cache'). Also, the file is non-HTML, so adding a 'no cache' meta tag to the file itself won't work. ...
0
9727
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10386
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10398
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10133
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9204
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6889
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4339
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.