pluf2

pluf2 Commit Details


Date:2008-11-11 02:23:28 (16 years 1 month ago)
Author:Loic d'Anterroches
Branch:master
Commit:fe2351e157c1dff04de9def07eeea1ace5c03755
Parents: 8305a3a4cc5398ac16519d85a7ebfb1add836d17
Message:Improved the file upload functionnalities.

- Added a new 'file_name' parameter to rename the uploaded file easily.
- Renamed the moveToUploadFolder function to match Pluf conventions.
Changes:

File differences

src/Pluf/Form/Field/File.php
2424
2525
2626
27
27
2828
2929
3030
......
8181
8282
8383
84
84
8585
8686
8787
......
9191
9292
9393
94
95
96
97
98
99
100
94101
95102
96103
......
99106
100107
101108
102
109
103110
104111
105112
......
111118
112119
113120
121
122
123
124
125
126
127
114128
115129
116130
class Pluf_Form_Field_File extends Pluf_Form_Field
{
public $widget = 'Pluf_Form_Widget_FileInput';
public $move_function = 'Pluf_Form_Field_moveToUploadFolder';
public $move_function = 'Pluf_Form_Field_File_moveToUploadFolder';
public $max_size = 2097152; // 2MB
public $move_function_params = array();
/**
* Default move function. The file name is sanitized.
*
* In the extra parameters, 2 can be used so that this function is
* In the extra parameters, options can be used so that this function is
* matching most of the needs:
*
* * 'upload_path': The path in which the uploaded file will be
*
* * 'upload_overwrite': Set it to true if you want to allow overwritting.
*
* * 'file_name': Force the file name to this name and do not use the
* original file name. If this name contains '%s' for
* example 'myid-%s', '%s' will be replaced by the
* original filename. This can be used when for
* example, you want to prefix with the id of an
* article all the files attached to this article.
*
* If you combine those options, you can dynamically generate the path
* name in your form (for example date base) and let this upload
* function create it on demand.
* @param array Extra parameters. If upload_path key is set, use it. (array())
* @return string Name relative to the upload path.
*/
function Pluf_Form_Field_moveToUploadFolder($value, $params=array())
function Pluf_Form_Field_File_moveToUploadFolder($value, $params=array())
{
$name = Pluf_Utils::cleanFileName($value['name']);
$upload_path = Pluf::f('upload_path', '/tmp');
throw new Pluf_Form_Invalid(__('An error occured when creating the upload path. Please try to send the file again.'));
}
}
if (isset($params['file_name'])) {
if (false !== strpos($params['file_name'], '%s')) {
$name = sprintf($params['file_name'], $name);
} else {
$name = $params['file_name'];
}
}
$dest = $upload_path.'/'.$name;
if ((!isset($params['upload_overwrite']) or $params['upload_overwrite'] == false) and file_exists($dest)) {
throw new Pluf_Form_Invalid(sprintf(__('A file with the name "%s" has already been uploaded.'), $name));

Archive Download the corresponding diff file

Branches

Number of commits:
Page rendered in 0.05349s using 13 queries.