tel8618217223380 / prado3

Automatically exported from code.google.com/p/prado3
Other
0 stars 0 forks source link

Feature Request: Provide a way to supply download filename to THttpResponse::writeFile() #176

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Please add a 5th, optional parameter to THttpResponse::writeFile(), where 
we can supply a filename for use in the "Content-Disposition" header field 
of the response. Currently, the filename is derived from the basename of 
the 1st parameter, which actually contains the local path of the file to 
be sent - but there are cases where an application want might to provide a 
different default name for the file to be saved. 

For ex. files might be stored under a random-generated filename on the 
server-side by the application to prevent any possible (file)name-based 
attacks by users of the site (much like files in the browser cache are 
stored in random-named directories), but we still might want to provide 
the user with the original name of the file when offering it to download. 

Currently, however, this is not possible, as the writeFile() method 
doesn't provide us a way to supply a default filename other than that of 
the name of the local file we want to sent. But by changing

  public function writeFile
($fileName,$content=null,$mimeType=null,$headers=null)

to

  public function writeFile
($fileName,$content=null,$mimeType=null,$headers=null,$saveName=null)

and

  $fn=basename($fileName);

to

 if (!$saveName) $fn = basename($fileName); else $fn = $saveName;

the method can be easily extended to provide that functionality too.

PS: Please don't tell me to simply load the contents of the file with 
file_get_contents() and supply it as the 2nd parameter to writeFile() 
while using the 1st param as the filename, because the memory footprint of 
that solution exceeds far that of a "regular" file download, as it 
requires PHP to actually load all of the file into the memory (which might 
be hundreds of MBs with a larger download), instead of just passing it to 
the output in block chunks, like it does with readfile().

Original issue reported on code.google.com by google...@pcforum.hu on 16 Jun 2009 at 2:39

GoogleCodeExporter commented 9 years ago
Fixed in 3.1 branch.

I've add 3 additional parameters to take more influence on behavior:
boolean $forceDownload: force download of file, even if browser able to display
inline. Defaults to 'true'.

string $fileNameClient: force a specific file name on client side. Defaults to 
'null'
means auto-detect.

integer fileSize: size of file or content in bytes if already known. Defaults to
'null' means auto-detect.

Original comment by GODZilla...@gmail.com on 27 Jun 2009 at 8:52