Force-Saving an Audio File
Force-Saving an Audio File
am 09.12.2009 03:36:16 von cool
Hi folks,
I'm trying to force save .mp3 files so this is a test page (found on
the net). It works fine when:
$directory = ""; // so the audio is in the same local directory
but fails when I use a REAL web directory - (the audio file is here
- http://mysite.com/test1/audio.mp3 )
$directory = "http://mysite.com/test1/";
says - The file $file was not found.
Q: Any ideas how to get it to download from the website?
=====
$file = 'audio1.mp3';
$directory = "http://mysite.com/test1/";
//$directory = "";
$path = "$directory$file";
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');
$file_extension = strtolower(substr(strrchr($path,"."),1));
if( $file == "" )
{
echo "
File not found.
File not found.
";
exit;
} elseif (! file_exists( $path ) )
{
echo "
The file $file was not found.
The file $file was not found.
- path - $path
";exit;
};
switch( $file_extension )
{
case "pdf": $ctype="application/pdf"; break;
case "zip": $ctype="application/zip"; break;
case "doc": $ctype="application/msword"; break;
case "xls": $ctype="application/vnd.ms-excel"; break;
case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break;
case "jpeg":
case "jpg": $ctype="image/jpg"; break;
case "wav":
case "mp3": $ctype="application/iTunes"; break;
default: $ctype="application/force-download";
}
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: $ctype");
header("Content-Disposition: attachment; filename=\"".basename
($path)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($path));
readfile("$path");
exit();
?>
Thanks,
cool@hosting4days.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Force-Saving an Audio File
am 09.12.2009 09:27:11 von Gaurav Kumar
--00504502aef702ca12047a4775f2
Content-Type: text/plain; charset=ISO-8859-1
You can do two things-
1. Read the file by providing a base directory path of the server where the
file exist.
Something like- //htdocs/httpfolder/files/audio/yourfile.mp3
You can take this in a variable " //htdocs/httpfolder/files/audio/".
2. Try using file_get_contents.
Something like $file = file_get_contents('
http://www.example.com/audio/file.mp3', false, $context);
Hope this helps.
Gaurav Kumar
Blog.OsWebStudio.com
On Wed, Dec 9, 2009 at 8:06 AM, cool@hosting4days.com
> wrote:
> Hi folks,
>
> I'm trying to force save .mp3 files so this is a test page (found on the
> net). It works fine when:
> $directory = ""; // so the audio is in the same local directory
>
> but fails when I use a REAL web directory - (the audio file is here -
> http://mysite.com/test1/audio.mp3 )
> $directory = "http://mysite.com/test1/";
>
> says - The file $file was not found.
>
> Q: Any ideas how to get it to download from the website?
>
> =====
>
>
>
>
>
> $file = 'audio1.mp3';
>
> $directory = "http://mysite.com/test1/";
> //$directory = "";
> $path = "$directory$file";
>
> if(ini_get('zlib.output_compression'))
> ini_set('zlib.output_compression', 'Off');
>
> $file_extension = strtolower(substr(strrchr($path,"."),1));
>
> if( $file == "" )
> {
> echo "
>
> File not found.
>
>
> File not found.
>
> ";
> exit;
> } elseif (! file_exists( $path ) )
> {
> echo "
>
> The file $file was not found.
>
>
> The file $file was not found.
> - path - $path
>
> ";exit;
> };
> switch( $file_extension )
> {
> case "pdf": $ctype="application/pdf"; break;
> case "zip": $ctype="application/zip"; break;
> case "doc": $ctype="application/msword"; break;
> case "xls": $ctype="application/vnd.ms-excel"; break;
> case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
> case "gif": $ctype="image/gif"; break;
> case "png": $ctype="image/png"; break;
> case "jpeg":
> case "jpg": $ctype="image/jpg"; break;
> case "wav":
> case "mp3": $ctype="application/iTunes"; break;
> default: $ctype="application/force-download";
> }
> 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: $ctype");
> header("Content-Disposition: attachment; filename=\"".basename($path)."\";"
> );
> header("Content-Transfer-Encoding: binary");
> header("Content-Length: ".filesize($path));
>
> readfile("$path");
> exit();
>
> ?>
>
>
>
> Thanks,
> cool@hosting4days.com
>
>
>
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--00504502aef702ca12047a4775f2--
Re: Force-Saving an Audio File
am 09.12.2009 17:39:04 von cool
On Dec 9, 2009, at 12:27 AM, Gaurav Kumar wrote:
> You can do two things-
>
> 1. Read the file by providing a base directory path of the server
> where the
> file exist.
> Something like- //htdocs/httpfolder/files/audio/yourfile.mp3
> You can take this in a variable " //htdocs/httpfolder/files/audio/".
>
> 2. Try using file_get_contents.
> Something like $file = file_get_contents('
> http://www.example.com/audio/file.mp3', false, $context);
>
> Hope this helps.
Thanks for your help Gaurav,
After a few tests based on your suggestions - I noticed why I think
there was a problem:
this line:
elseif (! file_exists( $path ) )
worked locally but failed when the path was a http url:
$directory = "http://mysite.com/test1/";
so when I took out that elseif then readfile("$path"); did work
SO: Is there a php function that can work like file_exists( $path )
and tell if the file exists but work from a URL like:
$thisfile = "http://mysite.com/test1/audio.mp3";
Thanks,
cool@hosting4days.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Force-Saving an Audio File
am 09.12.2009 17:46:52 von Shawn McKenzie
cool@hosting4days.com wrote:
>
> On Dec 9, 2009, at 12:27 AM, Gaurav Kumar wrote:
>
>> You can do two things-
>>
>> 1. Read the file by providing a base directory path of the server
>> where the
>> file exist.
>> Something like- //htdocs/httpfolder/files/audio/yourfile.mp3
>> You can take this in a variable " //htdocs/httpfolder/files/audio/".
>>
>> 2. Try using file_get_contents.
>> Something like $file = file_get_contents('
>> http://www.example.com/audio/file.mp3', false, $context);
>>
>> Hope this helps.
>
> Thanks for your help Gaurav,
>
> After a few tests based on your suggestions - I noticed why I think
> there was a problem:
>
> this line:
> elseif (! file_exists( $path ) )
> worked locally but failed when the path was a http url:
> $directory = "http://mysite.com/test1/";
>
> so when I took out that elseif then readfile("$path"); did work
>
> SO: Is there a php function that can work like file_exists( $path )
> and tell if the file exists but work from a URL like:
> $thisfile = "http://mysite.com/test1/audio.mp3";
>
>
>
>
> Thanks,
> cool@hosting4days.com
>
You could possibly check the results of get_headers($thisfile);
--
Thanks!
-Shawn
http://www.spidean.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Force-Saving an Audio File
am 10.12.2009 02:54:25 von cool
Thanks Gaurav & Shawn,
I'll check into your suggestions.
Thanks,
cool@hosting4days.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php