Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Archive /

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo

SWFUpload Extension


Go to End


2 Posts   2332 Views

Avatar
UncleCheese

Forum Moderator, 4102 Posts

16 November 2008 at 11:10am

Edited: 16/11/2008 11:15am

This extension allows the integration of the Flash-based upload utility SWFUpload for forms on the frontend or in the CMS.

Features:

- Displays upload progress in real time

- Interrogates file types and sizes in the dialog box. Ineligible files will be greyed out by the interface.

- Select multiple files at once for bulk uploading.

- Configuration is easy using the SWFUploadConfig API. No Javascript necessary.

- Flash 10 compatible.

Examples:

A frontend contact form:

function ResumeForm()
		return new Form(
			$this,
			"ResumeForm",
			new FieldSet(
				new TextField('Name','* Name'),
				new TextField('Address'),
				new TextField('City'),
				new TextField('State'),
				new NumericField('Zip'),
				new EmailField('Email','* Email'),
			        new SWFUploadField(
					"ResumeForm",
					"Resume",
					"* Please attach your most current copies of your resume and cover letter (PDF or DOC only). You must submit a resume to be considered.",
					array (
						'file_types_list' => '*.doc;*.pdf',
						'file_queue_limit' => '2',
						'browse_button_text' => 'Choose files...',
						'upload_url' => $this->Link('handleswfupload'),
						'required' => 'true'
					)
				)
			),
			new FieldSet(
				new FormAction('doResumeSubmit','Apply')
			),
			new RequiredFields('Name','College','YearOfGraduation','Email','Phone','Message')
		);
	}
}

/** Handle the upload anyway you want **/
	public function handleswfupload()
	{
		if (isset($_FILES["swfupload_file"]) && is_uploaded_file($_FILES["swfupload_file"]["tmp_name"])) {
	$file = new File();
	$file->loadUploaded($_FILES['swfupload_file']);
	   // store the owner id with the uploaded image
    	if($member = Member::currentUser())
		$file->OwnerID = $member->ID;
	$file->write();

/* this gets sent back to the form to an array of hidden inputs called "uploaded_files[]". When the form posts, you can handle the array of Files that were uploaded by their IDs.*/
        echo $file->ID; 

		} 
		else {
			echo ' '; // return something or SWFUpload won't fire uploadSuccess
		}
		
	}

Instead of passing the constructor the array of configuration vars, you can also do this before you instantiate the object:

SWFUploadConfig::addFileTypes(array('doc','pdf'));
SWFUploadConfig::set_var('file_queue_limit','2');
etc...

For the CMS:

public function getCMSFields()
{
$f = parent::getCMSFields();
$f->addFieldToTab("Root.Content.MyFile", new SWFUploadFileIFrameField('MyFile','My Label'));
return $f;
}

Avatar
Fuzz10

Community Member, 791 Posts

18 November 2008 at 9:12pm

This is brilliant !!

Thanks for the hard work ! I'll definitely try it out later today.