mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
Add ability to choose which file to upload to in a FileField
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@48778 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
b07725cdae
commit
97f3d722b4
@ -156,7 +156,7 @@ class File extends DataObject {
|
||||
/**
|
||||
* Save an file passed from a form post into this object
|
||||
*/
|
||||
function loadUploaded($tmpFile) {
|
||||
function loadUploaded($tmpFile, $folderName = 'Uploads') {
|
||||
if(!is_array($tmpFile)) user_error("File::loadUploaded() Not passed an array. Most likely, the form hasn't got the right enctype", E_USER_ERROR);
|
||||
if(!$tmpFile['size']) return;
|
||||
|
||||
@ -171,8 +171,8 @@ class File extends DataObject {
|
||||
if(!file_exists("$base/assets")){
|
||||
mkdir("$base/assets", Filesystem::$folder_create_mask);
|
||||
}
|
||||
if(!file_exists("$base/assets/Uploads")){
|
||||
mkdir("$base/assets/Uploads", Filesystem::$folder_create_mask);
|
||||
if(!file_exists("$base/assets/$folderName")){
|
||||
mkdir("$base/assets/$folderName", Filesystem::$folder_create_mask);
|
||||
}
|
||||
|
||||
// Generate default filename
|
||||
|
@ -11,6 +11,21 @@
|
||||
* @subpackage fields-files
|
||||
*/
|
||||
class FileField extends FormField {
|
||||
/**
|
||||
* Create a new file field.
|
||||
* @param string $name The internal field name, passed to forms.
|
||||
* @param string $title The field label.
|
||||
* @param int $value The value of the field.
|
||||
* @param Form $form Reference to the container form
|
||||
* @param string $rightTitle Used in SmallFieldHolder() to force a right-aligned label
|
||||
* @param string $folderName Folder to upload files to
|
||||
*/
|
||||
function __construct($name, $title = null, $value = null, $form = null, $rightTitle = null, $folderName = 'Uploads') {
|
||||
$this->folderName = $folderName;
|
||||
|
||||
parent::__construct($name, $title, $value, $form, $rightTitle);
|
||||
}
|
||||
|
||||
public function Field() {
|
||||
return
|
||||
$this->createTag("input", array("type" => "file", "name" => $this->name, "id" => $this->id())) .
|
||||
@ -26,7 +41,7 @@ class FileField extends FormField {
|
||||
return;
|
||||
|
||||
$file = new File();
|
||||
$file->loadUploaded($_FILES[$this->name]);
|
||||
$file->loadUploaded($_FILES[$this->name], $this->folderName);
|
||||
|
||||
$record->$fieldName = $file->ID;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user