Link to home
Start Free TrialLog in
Avatar of damijim
damijimFlag for United States of America

asked on

PHP Headers Renaming File to Send

Hello,
I have the code below. The script takes a wav file from an FTP server, then sends to the user's browser with a "randomly" generated file name.

This problem randomly started, but now every time the file that is sent to the user's browser is name get_call.wav. The issue is, get_call.php is name of the script doing this.

Am I using a wrong header? Why is this going on?!


Thanks!

//temporarily download the file to tmp/, send it, then delete it from tmp/
if (!ftp_get($ftp, "tmp/$random_string.wav",  $wav_name, FTP_BINARY)) {
   die("Error code: 7 - Please contact Client Services if the issue persists.");
   exit();
   } else {
 
   ftp_close($ftp); //close FTP connection
		
   //send the wav to client via headers
   $size = filesize("tmp/$random_string.wav");
   header("Content-Type: audio/wav", TRUE);
   header("Content-Disposition: ATTACHMENT; FILENAME=\"" . $random_string . ".wav\"", TRUE);
   header("Content-Length: " . $size);
   header("Content-Transfer-Encoding: binary");
   
//open, send file, and close connection, delete (unlink) the tmp file.
   $fh = fopen("tmp/$random_string.wav", "r");
   fpassthru($fh);
   fclose($fh);
   unlink("tmp/$random_string.wav");
}

Open in new window

Avatar of Scripting_Guy
Scripting_Guy
Flag of Switzerland image

first, make sure that $random_string is not 'get_call'. e.g., add if ($random_string == 'get_call') die ('weeeee');

Assuming that $random_string != 'get_call', then I think this is a browser / client issue. The browser has two options to chose from:

1. take the filename from Content-Disposition
2. take the actual filename from the url

On an apache2 webserver (with default config), you could prevent this by chaning your link to http://yourhost.com/get_wave.php/random_filename.wav. Apache will of course not find this file, but instead of giving a straight 404 error, it will recursivly go up level by level. so after not finding /get_wave.php/random_filename.wav, it will look for /get_wave.php, which it will find and execute. The URL however does not change.

You can then access the URI from inside your script over the $_SERVER['REQUEST_URI'] variable, which will still contain /get_wave.php/random_filename.wav. This apache behavior is very ofthen used for search engine friendly urls. If you had a news-script, you could call your news script with /news.php/1234/This-Is-The-Title-Of-The-Actual-News.htm. Obviously, the url is complete bs, but your newsscript knows it has to show the news with id 1234 and google just loves it if the title of a document is in the URL aswell.
I think it might the extra quotes in the filename, my examples didn't have quotes.

header("Content-Disposition: ATTACHMENT; FILENAME=\"" . $random_string . ".wav\"", TRUE);

if $random_string has no spaces then just try removing the quotes

header("Content-Disposition: ATTACHMENT; FILENAME=" . $random_string . ".wav", TRUE);

otherwise try single quotes

header("Content-Disposition: ATTACHMENT; FILENAME='" . $random_string . ".wav'", TRUE);


oh, and I bet your random problem isn't as random as you think. It's probably related to different browsers and possibly versions of the same browser.
Avatar of damijim

ASKER

Thanks both,
The weird thing is that it was sending it properly and not as "get_call.wav" but then stopped, then went back to it, now it's doing it again. I've had this same problem in the past and don't know what fixed it; I'm actually using the same code.

Anyway,
$random_string is a randomly generated string containing only alpha characters. I have checked.


Michael701:
Neither of those ideas worked. I don't think the Content-Disposition header is the error because if I remove that line completely, it will open QuickTime, WMP, etc, but the file is still named "get_call.wav"... (see attached code w/ that header removed, it'll play the wav in the client's default player as get_call.wav)..

if (!ftp_get($ftp, "tmp/$random_string.wav",  $wav_name, FTP_BINARY)) {
   die("Error code: 7 - Please contact Client Services if the issue   persists.");
   exit();
} else {
   ftp_close($ftp); //close FTP connection
		
   //send the wav to client via headers
   $size = filesize("tmp/$random_string.wav");
   header("Content-Type: audio/wav", TRUE);
   header("Content-Length: " . $size);
   header("Content-Transfer-Encoding: binary");
   
   //open, send file, and close connection, delete (unlink) the tmp file.
   $fh = fopen("tmp/$random_string.wav", "r");
   fpassthru($fh);
   fclose($fh);
   unlink("tmp/$random_string.wav");
}

Open in new window

Avatar of damijim

ASKER

Scripting_Guy,
I kind of follow what you are saying, but not really sure how to do what you are talking about. Thanks.
Avatar of damijim

ASKER

Test:
print("tmp/$random_string.wav");die();

Output:
tmp/68y92A02X04178657o52.wav
OK, I thought you were having a problem with the 'save as' download file name.

"it will open QuickTime, WMP, etc, but the file is still named "get_call.wav""

It seems like you're downloading the file just fine with random.wav file name. But when you play the wav the title is in the player get_wav.php

Is this correct?

Then you'll have to look at the wav properties. Just like MP3 tags. these are embedded within the wav file. How is the file created?
Avatar of damijim

ASKER

Either way I take, the file name is get_call.wav whether I let it play or force download.

The file name is sends if I force download is "get_call.wav" via Save As, Open, etc.

If I have it automatically play in the browser, I can see in WMP that the name is get_call.wav ...

So, it is actually sending the file as get_call.wav ....
Avatar of damijim

ASKER

Even if I use this header:

header("Content-Disposition: ATTACHMENT; FILENAME='cat.wav'", TRUE);

it sends me get_call.wav .... the name of the PHP script (get_call.php) that is sending the file
Avatar of damijim

ASKER

ACK, of course it works in FF now... it's IE6 that it is not working on...
Remember how I said it's probably a browser issue?

http://www.cert.org/advisories/CA-2001-36.html

Is this your machine? Do you have all patches for IE6 installed?
Avatar of damijim

ASKER

Well, it's my company's machine, but I have local admin rights. The web server is hosted by a server in the server room.

I have all the patches for IE6 (and Windows Updates). I've tried it on several IE6 PC's here at work and it send "get_call.wav" on all of them. It used to send the correct file name though.

I read the link, but that's just patching my computer. How can I resolve the issue for anyone who uses IE? It doesn't provide a work-a-round.

So, how can I resolve this?

Thanks for your help!
IE.png
FF.png
Sorry, but the answer to this is NO there doesn't seem to be a work around because it's a feature NOT supported in IE6.
Avatar of damijim

ASKER

So, I cannot control the name of a file that is being sent?
ASKER CERTIFIED SOLUTION
Avatar of Michael701
Michael701
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of damijim

ASKER

I accomplished it by using pathinfo.inc...

More information at http://richardlynch.blogspot.com/2006_06_01_archive.html
cool, request that this question be closed and the points refunded to your account.
Avatar of damijim

ASKER

I have a Premium account so I have unlimited points. I appreciate your effort in assisting me with this, so here are the points. Thanks ;)