Skip to content

Commit

Permalink
(Fixes issue 3154) xSendFile - removed file existance check and added…
Browse files Browse the repository at this point in the history
… additional headers option

git-svn-id: http://yii.googlecode.com/svn/trunk@3556 c7f931c7-7552-0410-b027-2fb3d89d9100
  • Loading branch information
mdomba committed Feb 9, 2012
1 parent 08481a1 commit 34aa6a3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Expand Up @@ -16,6 +16,7 @@ Version 1.1.10 work in progress
- Enh #3101: The methods of CSecurityManager do now work correctly for the case that mbstring.func_overload is in effect (Y!!)
- Enh #3112: Fixed the exception error display on ajax calls when YII_DEBUG is true (mdomba)
- Enh #3121: Added more rules for proper pluralization to the pluralize() method in CCodeModel and CConsoleCommand (mdomba)
- Enh #3154: Removed file existance check to allow relative path and added additional headers option to xSendFile() (mdomba)
- Enh #3169: Added CSort::SORT_ASC and CSort::SORT_DESC (Sam Dark)
- Enh: Added CActiveForm::validateTabular() to simplify ajax validation for tabular input (mdomba)
- Chg: HTML-encoded input values for exist and unique validators (Qiang)
Expand Down
4 changes: 3 additions & 1 deletion UPGRADE
Expand Up @@ -21,7 +21,9 @@ General upgrade intructions

Upgrading from v1.1.9
---------------------

- Previously xSendFile() was returning false if the file was not found.
This has been removed to allow relative file paths. If you are relying on this check,
you will need to do it manually before calling xSendFile().

Upgrading from v1.1.8
---------------------
Expand Down
29 changes: 15 additions & 14 deletions framework/web/CHttpRequest.php
Expand Up @@ -811,29 +811,26 @@ public function sendFile($fileName,$content,$mimeType=null,$terminate=true)
* <b>Example</b>:
* <pre>
* <?php
* Yii::app()->request->xSendFile('/home/user/Pictures/picture1.jpg',array(
* 'saveName'=>'image1.jpg',
* 'mimeType'=>'image/jpeg',
* 'terminate'=>false,
* ));
* Yii::app()->request->xSendFile('/home/user/Pictures/picture1.jpg',array(
* 'saveName'=>'image1.jpg',
* 'mimeType'=>'image/jpeg',
* 'terminate'=>false,
* ));
* ?>
* </pre>
* @param string $filePath file name with full path
* @param array $options additional options:
* <ul>
* <li>saveName: file name shown to the user, if not set real file name will be used</li>
* <li>mimeType: mime type of the file, if not set it will be guessed automatically based on the file name.</li>
* <li>mimeType: mime type of the file, if not set it will be guessed automatically based on the file name, if set to null no content-type header will be sent.</li>
* <li>xHeader: appropriate x-sendfile header, defaults to "X-Sendfile"</li>
* <li>terminate: whether to terminate the current application after calling this method, defaults to true</li>
* <li>forceDownload: specifies whether the file will be downloaded or shown inline. Defaults to true. (Since version 1.1.9.)</li>
* <li>forceDownload: specifies whether the file will be downloaded or shown inline, defaults to true. (Since version 1.1.9.)</li>
* <li>addHeaders: an array of additional http headers in header-value pairs</li>
* </ul>
* @return boolean false if file not found, true otherwise.
*/
public function xSendFile($filePath, $options=array())
{
if(!is_file($filePath))
return false;

if(!isset($options['forceDownload']) || $options['forceDownload'])
$disposition='attachment';
else
Expand All @@ -851,14 +848,18 @@ public function xSendFile($filePath, $options=array())
if(!isset($options['xHeader']))
$options['xHeader']='X-Sendfile';

header('Content-type: '.$options['mimeType']);
if($options['mimeType'] !== null)
header('Content-type: '.$options['mimeType']);
header('Content-Disposition: '.$disposition.'; filename="'.$options['saveName'].'"');
if(isset($options['addHeaders']))
{
foreach($options['addHeaders'] as $header=>$value)
header($header.': '.$value);
}
header(trim($options['xHeader']).': '.$filePath);

if(!isset($options['terminate']) || $options['terminate'])
Yii::app()->end();

return true;
}

/**
Expand Down

0 comments on commit 34aa6a3

Please sign in to comment.