[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2006-04-21
17
Medium Priority
?
668 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:vn4phuong
  • 6
  • 5
  • 3
14 Comments
 
LVL 3

Accepted Solution

by:
NeoEGM earned 672 total points
ID: 16515243
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
 
LVL 6

Assisted Solution

by:nehaya
nehaya earned 664 total points
ID: 16515262
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
 
LVL 6

Expert Comment

by:nehaya
ID: 16515287
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 3

Expert Comment

by:NeoEGM
ID: 16515336
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
 
LVL 6

Expert Comment

by:nehaya
ID: 16515373
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
 
LVL 3

Expert Comment

by:NeoEGM
ID: 16515419
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
 
LVL 6

Assisted Solution

by:WilliamFrantz
WilliamFrantz earned 664 total points
ID: 16518239
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
 
LVL 3

Expert Comment

by:NeoEGM
ID: 16519874
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
 
LVL 6

Expert Comment

by:nehaya
ID: 16520017
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
 
LVL 6

Expert Comment

by:WilliamFrantz
ID: 16520509
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
 
LVL 6

Expert Comment

by:nehaya
ID: 16522311
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
 
LVL 6

Expert Comment

by:WilliamFrantz
ID: 16526183
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
 
LVL 3

Expert Comment

by:NeoEGM
ID: 16830436
I think I gave the user a valid solution...

Regards,
NeoEGM
0
 
LVL 6

Expert Comment

by:nehaya
ID: 16831747
Why you don't split the points?
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Originally, this post was published on Monitis Blog, you can check it here . It goes without saying that technology has transformed society and the very nature of how we live, work, and communicate in ways that would’ve been incomprehensible 5 ye…
Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month20 days, 2 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question