> Hey guys,
>
> I need to allow users to upload files. Thing is, I am not using the SF
> Forms, and my forms are as basic as they can be. I want to make use of
> something that is the equivalent of $this->getrequest()->moveFile() but that
> seems to be deprecated.
>
I don't understand your question. If you do not want to use Symfony
forms, then you would just write your upload code as if it was normal
PHP code. I mean, you would use whatever PHP code you normally use for
uploading files.
When I'm not using Symfony forms, I have often used code like this:
if ($request->getFiles('question')) {
$uploadedFileArray = $request->getFiles('question');
// $pathToTemporaryFile will equal something like
"/home/wpquestions/tmp/phpm9Lnd4"
$pathToTemporaryFile = $uploadedFileArray['tmp_name']['image'];
$fileMimeType = $uploadedFileArray['type']['image'];
$fileOriginalName = $uploadedFileArray['name']['image'];
$fileUploadError = $uploadedFileArray['error']['image'];
// what if the user does not upload an image? we do not want
this field blanked, if they have
// previously uploaded an image, so lets save this value
$previousImage = $question->getImage();
$newImage = new Image();
$nameOfNewProfileImageSuccessfullyUploaded =
$newImage->executeUpload($pathToTemporaryFile, $fileMimeType,
$this->getUser()->getUsername(), $fileOriginalName);
if ($nameOfNewProfileImageSuccessfullyUploaded) {
$question->setImage
($nameOfNewProfileImageSuccessfullyUploaded);
// what follows is for creating a user gallery of images
$newImage->setImageName
($nameOfNewProfileImageSuccessfullyUploaded);
$newImage->setUserId($this->getUser()->getGuardUser()->getId
());
$newImage->save();
} else {
$this->logMessage("In question/executeStep3 we tried to save
'$pathToTemporaryFile' but upload or saving failed.", 'debug');
}
// if the user didn't upload an image, then reset the image name
to whatever existed previously
// ($fileUploadError will equal 0 if there were no errors, but if
the user didn't upload anything, this
// equal 4
if ($fileUploadError) {
$question->setImage($previousImage);
}
}
To resize the image upon upload, I use both the eCrop plugin, and the
sfThumbnail plugin, which I use inside my Image model class, like so:
/*
* 2009-08-29 -
*
* $absolutePathToFile will equal something like
"/home/wpquestions/tmp/phpm9Lnd4"
*
* this is being called in content/updateUser
*
*/
public function executeUpload($absolutePathToFile, $fileMimeType,
$username, $fileOriginalName)
{
if ($fileOriginalName) {
$username = trim($username);
$username = str_replace(" ", "_", $username);
$fileName = basename($absolutePathToFile);
$originalFileExtensionArray = explode(".", $fileOriginalName);
$originalFileExtension = array_pop($originalFileExtensionArray);
$newNameForBaseImage = $username.'_'.$fileName.'.'.
$originalFileExtension;
if (!stristr($fileOriginalName, ".bmp")) {
$c = new eCrop($absolutePathToFile);
$c->cropLargestSquareArea(sfConfig::get('sf_upload_dir').'/
crop_'.$newNameForBaseImage,
eCrop::CROP_CENTER, eCrop::CROP_CENTER, false);
$c->setThumbSize(48, 48);
$c->cropLargestSquareArea(sfConfig::get('sf_upload_dir').'/
crop_small_'.$newNameForBaseImage,
eCrop::CROP_CENTER, eCrop::CROP_CENTER);
$c = new eCrop($absolutePathToFile);
$c->setThumbSize(160, 160);
$c->cropLargestSquareArea(sfConfig::get('sf_upload_dir').'/
crop_medium_'.$newNameForBaseImage,
eCrop::CROP_CENTER, eCrop::CROP_CENTER);
$c = new eCrop($absolutePathToFile);
$c->setThumbSize(300, 300);
$c->cropLargestSquareArea(sfConfig::get('sf_upload_dir').'/
crop_bigger_'.$newNameForBaseImage,
eCrop::CROP_CENTER, eCrop::CROP_CENTER);
// Create the medium thumbnail
$thumbnail = new sfThumbnail(300, 300);
$thumbnail->loadFile($absolutePathToFile);
$thumbnail->save(sfConfig::get('sf_upload_dir').'/bigger_'.
$newNameForBaseImage,
$fileMimeType);
// Create the medium thumbnail
$thumbnail = new sfThumbnail(160, 160);
$thumbnail->loadFile($absolutePathToFile);
$thumbnail->save(sfConfig::get('sf_upload_dir').'/medium_'.
$newNameForBaseImage,
$fileMimeType);
// Create the small thumbnail
$thumbnail = new sfThumbnail(80, 80);
$thumbnail->loadFile($absolutePathToFile);
$thumbnail->save(sfConfig::get('sf_upload_dir').'/small_'.
$newNameForBaseImage,
$fileMimeType);
}
// Move the uploaded file to the 'uploads' directory
$success = copy($absolutePathToFile,
sfConfig::get('sf_upload_dir').'/'.$newNameForBaseImage);
if ($success) {
return $newNameForBaseImage;
} else {
return false;
}
}
}
If you were using Symfony forms, you would probably put this code in
the form class, rather than the model.
On Nov 26, 6:59 pm, Parijat Kalia <[email protected]> wrote:
> IT requires me to use SF forms...I want to do it without using that....any
> other suggestions?? Thanks for the link though!
>
> On Thu, Nov 26, 2009 at 12:13 PM, Martin Ibarra Cervantes <
>
> [email protected]> wrote:
> > please see
> >http://stereointeractive.com/blog/2009/01/23/uploading-a-file-with-sy...
>
> > or the documentation on symfony is very easy
>
> > On Wed, Nov 25, 2009 at 10:38 PM, Parijat Kalia <[email protected]>
> > wrote:
> > > Hey guys,
>
> > > I need to allow users to upload files. Thing is, I am not using the SF
> > > Forms, and my forms are as basic as they can be. I want to make use of
> > > something that is the equivalent of $this->getrequest()->moveFile() but
> > that
> > > seems to be deprecated.
>
> > > Thanks a lot guys!
>
> > > --
>
> > > You received this message because you are subscribed to the Google Groups
> > > "symfony users" group.
> > > To post to this group, send email to [email protected].
> > > To unsubscribe from this group, send email to
> > > [email protected]<symfony-users%[email protected]>
> > .
> > > For more options, visit this group at
> > >http://groups.google.com/group/symfony-users?hl=en.
>
> > --
>
> > You received this message because you are subscribed to the Google Groups
> > "symfony users" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to
> > [email protected]<symfony-users%[email protected]>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/symfony-users?hl=en.
--
You received this message because you are subscribed to the Google Groups
"symfony users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/symfony-users?hl=en.