Reply to Re: how do I tell a browser the name of a file, for download, if the file has open space in its name?

Your name:

Reply:


Posted by lawrence k on 10/20/07 17:03

On Oct 20, 12:44 pm, lawrence k <lkrub...@geocities.com> wrote:
> On Oct 20, 6:33 am, The Natural Philosopher <a...@b.c> wrote:
>
>
>
> > lawrence k wrote:
> > > 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):
>
> > > $fileToBuy = $_GET["fileToBuy"];
> > > if ($fileToBuy) {
> > > $pathToFile = "temporary_files/$fileToBuy";
> > > if (!file_exists($pathToFile)) $pathToFile = "site_specific_files/
> > > $fileToBuy";
> > > if (!file_exists($pathToFile)) $pathToFile = "../httpdocs/
> > > site_specific_files/$fileToBuy";
>
> > > if (file_exists($pathToFile)) {
> > > $size = @ filesize($pathToFile);
> > > if ($size > 1) {
> > > header("Content-Type: application/octet-stream");
> > > header("Content-Length: $size");
> > > header("Content-Disposition: attachment; filename=$fileToBuy");
> > > header("Content-Transfer-Encoding: binary");
> > > $pathToFile = urlencode($pathToFile);
> > > $fh = fopen("$pathToFile", "r");
> > > fpassthru($fh);
> > > } else {
> > > echo "Sorry, but we are unable to process this file at this
> > > time.";
> > > }
> > > } else {
> > > echo "Sorry, but we can not find a file named '$fileToBuy' at
> > > '$pathToFile'. ";
> > > }
> > > } else {
> > > echo "Sorry, but there doesn't seem to be a file named in the URL
> > > (fileToDownload needed in url).";
> > > }
>
> > > This works fine except when it encounters a file with an open space in
> > > it, and the studio has several thousand mp3s which have open spaces in
> > > their name.
>
> However, I've added an if() test to see if I truly did get a file
> handle back from fopen(). The strange thing is, this test does not
> fail, meaning PHP was able to find the file. Check out this url:
>
> http://www.monkeyclaus.org/download.php?fileToDownload=Diario%20-%20T...
>
> The code now looks like this:
>
> $fileToBuy = $_GET["fileToBuy"];
> if (!$fileToBuy) $fileToBuy = $_GET["fileToDownload"];
>
> if ($fileToBuy) {
> $pathToFile = "temporary_files/$fileToBuy";
> if (!file_exists($pathToFile)) $pathToFile = "site_specific_files/
> $fileToBuy";
> if (!file_exists($pathToFile)) $pathToFile = "../httpdocs/
> site_specific_files/$fileToBuy";
>
> if (file_exists($pathToFile)) {
> $size = @ filesize($pathToFile);
> if ($size > 1) {
> header("Content-Type: application/octet-stream");
> header("Content-Length: $size");
> header("Content-Disposition: attachment; filename=$fileToBuy");
> header("Content-Transfer-Encoding: binary");
> $fh = fopen("$pathToFile", "r");
> if ($fh) {
> fpassthru($fh);
> } else {
> echo "Error: can't find file '$fileToBuy' ";
> }
> } else {
> echo "Sorry, but we are unable to process this file at this
> time.";
> }
> } else {
> echo "Sorry, but we can not find a file named '$fileToBuy' at
> '$pathToFile'. ";
> }} else {
>
> echo "Sorry, but there doesn't seem to be a file named in the URL
> (fileToDownload needed in url).";
>
> }
>
> Why is the file name getting truncated? Is this an HTTP header issue?


Okay, it was an HTTP header issue. I've changed the code like this:


header("Content-Type: application/octet-stream");
header("Content-Length: $size");
$fileToBuy = rawurlencode($fileToBuy);
header("Content-Disposition: attachment; filename=$fileToBuy");
header("Content-Transfer-Encoding: binary");
$fh = fopen("$pathToFile", "r");
if ($fh) {
fpassthru($fh);
} else {
echo "Error: can't find file '$fileToBuy' ";
}



Now that $fileToBuy is hit with rawurlencode before getting put into
the HTTP header, the file downloads correctly. I was able to download
a file and play it in iTunes.

However, the file downloads with a bunch of ugly %20 in the file
names. Can anyone think of a way I can get them out of the filenames?

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация