|
Posted by Puckdropper on 10/20/07 17:01
lawrence k <lkrubner@geocities.com> wrote in
news:1192898654.783609.289650@e34g2000pro.googlegroups.com:
> 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.
>>
>> > If a file has an open space, the above code seems to truncate the
>> > file name at the first open white space. Instead of "animal
>> > hospital.mp3", the browser starts to download "animal", with no
>> > file extension. And the download fails - instead of 6 megabytes,
>> > only 1 kilobyte downloads.
>>
>> > Any insights?
>>
>> Yu are looking n the wrong place.
>>
>> is what happens before the first line that interests me
>>
>> $fileToBuy = $_GET["fileToBuy"];
>>
>> How is that GET variable generated, and what does
>>
>> echo $filetobuy;
>>
>> display?
>>
>> My guess is the name is truncated before it even reaches the part you
>> showed us.
>
>
> If I echo $fileToBuy to screen, I get the whole the file name. All the
> file_exists() tests would fail, if $_GET wasn't returning the whole
> file 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-%20Thi
> ngs%20In%20the%20Mirror%20Appear%20Closer%20Than%20They%20Are%20-%2004%
> 20-%20Coins%20Coins%20Coins.mp3
>
>
> 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?
>
>
I solved a similar problem by making sure all my fields had quotation
marks around them. Your filename field is the most likely to need them,
if allowed in the header.
Puckdropper
--
Wise is the man who attempts to answer his question before asking it.
To email me directly, send a message to puckdropper (at) fastmail.fm
[Back to original message]
|