how to save a mp3 file from the site without "right mouse click, then save target as" ?

Hi,
I would like somebody come to my website and able to download the mp3 file. However, I don't want them to right click then save target as and save the file.  All I want is just click the link the automatically download.
Please see the link below.

http://viet1800.com/music/getdownload.php?id=200642261028&language=english&numberdownload=2
the reason is that I will disable right click so people don't see the music file link so they don't link the file from another website.

Thanks
vn4phuongAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NeoEGMCommented:
You can't do it directly cause you have to tell the browser it's an attachment...

If you're using PHP, you can call a PHP script just like:

http://www.yoursite.com/downloadfile.php?file=FILE_PATH

instead of directly linking to the MP3, and then, in the script, call this function:

-------------------------- CODE -----------------------------
<?php
function dl_file_resume($file){

   //First, see if the file exists
   if (!is_file($file)) { die("<b>404 File not found!</b>"); }
   
   //Gather relevent info about file
   $len = filesize($file);
   $filename = basename($file);
   $file_extension = strtolower(substr(strrchr($filename,"."),1));
   
   //This will set the Content-Type to the appropriate setting for the file
   switch( $file_extension ) {
       case "exe": $ctype="application/octet-stream"; break;
       case "zip": $ctype="application/zip"; break;
       case "mp3": $ctype="audio/mpeg"; break;
       case "mpg":$ctype="video/mpeg"; break;
       case "avi": $ctype="video/x-msvideo"; break;
       default: $ctype="application/force-download";
   }
   
   //Begin writing headers
   header("Cache-Control:");
   header("Cache-Control: public");
   
   //Use the switch-generated Content-Type
   header("Content-Type: $ctype");
   if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) {
       # workaround for IE filename bug with multiple periods / multiple dots in filename
       # that adds square brackets to filename - eg. setup.abc.exe becomes setup[1].abc.exe
       $iefilename = preg_replace('/\./', '%2e', $filename, substr_count($filename, '.') - 1);
       header("Content-Disposition: attachment; filename=\"$iefilename\"");
   } else {
       header("Content-Disposition: attachment; filename=\"$filename\"");
   }
   header("Accept-Ranges: bytes");
   
   $size=filesize($file);
   //check if http_range is sent by browser (or download manager)
   if(isset($_SERVER['HTTP_RANGE'])) {
       list($a, $range)=explode("=",$_SERVER['HTTP_RANGE']);
       //if yes, download missing part
       str_replace($range, "-", $range);
       $size2=$size-1;
       $new_length=$size2-$range;
       header("HTTP/1.1 206 Partial Content");
       header("Content-Length: $new_length");
       header("Content-Range: bytes $range$size2/$size");
   } else {
       $size2=$size-1;
       header("Content-Range: bytes 0-$size2/$size");
       header("Content-Length: ".$size);
   }
   //open the file
   $fp=fopen("$file","rb");
   //seek to start of missing part
   fseek($fp,$range);
   //start buffered download
   while(!feof($fp)){
       //reset time limit for big files
       set_time_limit(0);
       print(fread($fp,1024*8));
       flush();
       ob_flush();
   }
   fclose($fp);
   exit;
}
?>
-----------------------------------------------------------

with the filename as the parameter...

This function will also workaround some problems that could arise with different browser implementation issues...

Hope it helps,

Regards,
NeoEGM
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nehayaCommented:
If they click directly, they will play it inside the browser!
- Even so, it's easy to view the source of the page, and they will see the link!
- The only way to do to solve linking problem, is by providing tokens (famous way) :
.. you application should generate very long hashed values.. let' say 100,000 hashed value..
each time a person view the page, the download link will be like this:
http://....../music/download.php?id=(here music id)&token=a897ffa78788bfd78fds09ds09675765675gaafr097879870934
and in download page, the token value will be checked, if it's found, remove it from DB, and give the uer the file.. else tell him to visit the website and try again..
this way, only one person can use the hashvalue. and he cannot use it again..
- you can genertate another 100,000 when the first group about to all gone

Regards,
Nehaya
0
nehayaCommented:
to NeoEGM:
the same problem exists!
He cannot prevent people form direcatly put the link in their websites!
They will put the same link:http://www.yoursite.com/downloadfile.php?file=FILE_PATH and get away with it!

Regards,
Nehaya
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

NeoEGMCommented:
nehasha:

If you re-read what vn4phuong wrote, you'd notice he was not asking how to prevent the user from direct linking the MP3 file, but how to make it download directly... and that's what I answered...

If he wanted to implement a better way of preventing it, he could just do it in the downloadfile.php...

Regards,
NeoEGM
0
nehayaCommented:
NeoEGM:

- See his last line..

> If he wanted to implement a better way of preventing it, he could just do it in the downloadfile.php
Ok! I didn't say no! but i said, if he can implment the token idea INSIDE the download page, he will solve it..

Regards,
Nehaya


0
NeoEGMCommented:
nehasha:

Surely... but sometimes it isn't necessary to complicate a method... you have to measure the kind of users that will access your site and how many of them would try to directly link the files (and how much you don't want it to happen)...

If there are so many that know how to dig in the source code, then you could try to implement a better approach... if, otherwise, you could just keep 75% of them from linking, then it's not bad to disable the right click...

Regards,
NeoEGM
0
WilliamFrantzCommented:
I'm not sure why NeoEGM's function is so long and complex, but I use a shorter function for downloading files.

<?php
function sendFile($filename)
{
        /* required for IE, otherwise Content-disposition is ignored */
        if(ini_get('zlib.output_compression')) ini_set('zlib.output_compression', 'Off');

        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Cache-Control: private",false);
        header("Content-Type: application/octet-stream");
        header("Content-Disposition: attachment; filename=\"".basename($filename)."\";");
        header("Content-Transfer-Encoding: binary");
        header("Content-Length: ".@filesize($filename));
        set_time_limit(0);
        @readfile($filename);
        exit;
}
?>
0
NeoEGMCommented:
WilliamFrantz:

Take a look at the function and you'll see it handles better the filetypes (so it can give more information to the browser) and also workaround some user-agent bugs...

You can see different versions and explanations here: http://www.php.net/function.fread

Regards,
NeoEGM
0
nehayaCommented:
Can I ask a question?

If I (in both of your functions) do "right mouse click" , and choose save target as, what is going to happen?!
from what I see, the file is going to be downloaded as always!

Please, read the question of "vn4phuong" again!

Regards,
Nehaya
0
WilliamFrantzCommented:
nehaya, I think your interpretation is, "I don't want to allow visitors to right click..." which is a completely different problem.  Here's my interpretation of the question:

> I don't want [visitors] to [have to] right click then save target as [in order to] save
> the file.  I want [visitors to] just [left] click the link [to start the] download [without
> needing to use the pop-up menu].

NeoEGM, in this case, you don't actually want to send any MIME type other than "application/octet-stream".  If you provide a MIME type, the browser will try to open the file rather than forcing the user to save the file locally.  Again, that's my interpretation of the question.

vn4phuong, please clarify your request if I've got it wrong.
0
nehayaCommented:
WilliamFrantz:
See the last Line , Please!
> the reason is that I will disable right click so people [don't see the music file link] so they [don't link the file from another website].

Regards,
Nehaya
0
WilliamFrantzCommented:
Nehaya, I read that as:

> the reason is that I [plan to] disable right click...

Rather than

> the reason [I need to] disable right click...

Sorry.  The extra "is that" through me off.  However, if vn4phuong is trying to prevent hot-linking then simply disabling the context menu won't do any good.  As Nehaya already pointed out, you need to use tokens that expire or at least check the HTTP referrer headers.

Here's a tutorial about how to prevent hotlinking: http://www.htmlcenter.com/tutorials/tutorials.cfm/159/PHP/

Here's an example of how to disable the context menu, even though it won't prevent hotlinks:

<html>
<head>Your page title</head>
<body oncontextmenu="return false;">
    Your content goes here.
</body>
</html>
0
NeoEGMCommented:
I think I gave the user a valid solution...

Regards,
NeoEGM
0
nehayaCommented:
Why you don't split the points?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.