fUpload::move() destination file name / Suggestions Anyone?
would it make sense to have an optional parameter in fUpload::move() for the destination file name?
i can see a use case that makes an obvious conflict - say your destination directory is full of images named with a number that corresponds to a database entry's index, and then someone wants to upload a file they might have named '2.jpg'. if you implemented fUpload as documented, the user is going to get an erroneous conflict error, or worse yet they're going to inadvertently overwrite an existing '2.jpg' if you set enableOverwrite().
of course there are ways around this case, but it seems to me fUpload::move() is the simpler place to resolve it, no?
-
Message #72
you know, i should learn to read a bit more. fUpload::move() renames the file on conflict, and i should be using fORMFile anyway!
sorry about that :)
vena03/07/09 13:19:17 -
Message #74
Not a problem. In case you do need to use fUpload without the ORM, it does have an option you can set to overwrite a file when being moved.
In addition, you can manually rename a file after the move with code such as below. Since fUpload::move() returns an fFile (or fImage) object, you can chain a method call from the return value.
// Ensure the file is always temp.jpg $upload = new fUpload(); $temp_jpg = $upload->move($dir, 'file')->rename($dir . 'temp.jpg');
wbond03/07/09 22:22:43 -
-
Message #161
Sorry to bump an old thread, but i'm wondering if you could tell me how one might make fORMFile use the primary key as the filename?
thank you!
vena03/28/09 22:58:46 -
-
Message #163
You will likely need to write a callback and register it with the fActiveRecord hook 'post::store()'. You can see the page fORM#AddingFunctionalitytofActiveRecord for more info about adding functionality to fActiveRecord.
Here is a quick little example that should work, however I have not tested it:
class User extends fActiveRecord { static public function renameImage($object, &$values, &$old_values, &$related_records, &$cache) { if ($object->getImage()) { $info = fFilesystem::getPathInfo( $object->getImage()->getFilename() ); if ($info['filename'] != $object->getUserId()) { $image->rename($info['dirname'] . '/' . $user->getUserId() . '.' . $info['extension']); $user->store(); } } } protected function configure() { fORMFile::configureFileUploadColumn($this, 'image', '/path/to/dir'); fORM::registerHookCallback($this, 'post::store()', array('User', 'renameImage')); } }
On a side note, this example (and another post in the forums here) shows that it would be a little nicer if fImage::rename() would allow just a filename rather than a whole file path. I'll open a ticket for that so that it gets done.
wbond03/28/09 23:31:25 -
-
Message #184
Hey Will, I tried your example (with a few tweaks, as you're using variables like $image and $user which were never created :) ), but it seems to only work for existing records. If the record is just being created, an fValidation exception is thrown saying that a record with the same primary key already exists. It seems like there's some problem using store() in a post::store() callback…
Any idea what could be done differently here?
Thanks!
vena04/15/09 17:54:13
-
-
-
