Upload Multiple Files On Insert

Hi,

Please I really need some advice to implement this. Don’t even need the code… I just need some ideas.

When user is creating a new record, I don’t know the id of this to link the uploaded files. How can I link the the uploaded files to this record? I think maybe I can upload the files and store the files IDs on a session and then after user saves the record I’m able to link them. But I don’t think this is right. Can someone help me? Have anyone done this before?

Thanks

4542

SAMPLE.png

Hi

After of saving record the object has received the id. you can use it!

but how store the files in database (name of file)? give us more detail about the task

Hi @KonApaz, actually I know I can use the id after save the record on controller but since the user will upload the files on the create form before saves it, I just can’t use the ID.

I was thinking to let the user complete the form, and if him need to attach files I call save method on the upload action and then link the id to files.

What do u think?

If you want the user uploads first the files and after save the record, you could do this:

save the name of uploaded files on user state


$files = Yii::app()->user->getState('uploadedFiles' , '');

Yii::app()->user->setState('uploadedFiles' , $files . ',' . $newNamefile);



After of uploading of files, store the record, now you have the id

after of that save the file names in database with the related id like that


$files = Yii::app()->user->getState('uploadedFiles' , '');

$allfiles = explode(",", $files)

foreach ($allfiles as $file) {

  //save each file with stored id

}

Yii::app()->user->setState('uploadedFiles' , '');



Thanks dude. That’s what I will do.

Storing record-level data in session may lead to some unexpected behavior.

For example:

  • user has several windows or tabs opened at the same time

  • user opens the ‘create’ url, uploads some files and then closes the window

In these cases files from one form can be attached to another by mistake.

PS. I prefer using UUIDs for knowing ID in advance.

files can be a hash name after upload, so in this case there is no posibilities (depended by hash algorithm) to attached by another user by mistake

If you want more secured way you can use a temporar table that stores the unattached files

the table must have at least two fields: user_id and namefile (hashed)

but in this case if the user uploads files without creat es record, the next time these files will be attached with next record!

The most secured way is to attach the files with the record at the same time, all in one form

I’m not talking about another user (their sessions differ so there’s no problem), I’m talking about the same user but different records.

Sessions are quite unreliable, so there are a lot of situations to consider before doing it this way.

about timeout of session I mentioned that because in this case the files will haven’t any record!

The unique Id you suggested, I thing, has the same problem with sessions. How to generate a unigue Id when the user will going to create two records after of uploaded files? all the files will be attached to the first saved record,

please explain your solution, maybe I didn’t understand something :)

As I mentioned this is trick. I agree that sessions may be a concern as we can’t always antecipate user actions.

Always the convertations improve ourselves even though there are variaty of opinions or disagreements :)

Also thanks a lot for your votes :)

Ok, here’s how I do it.

I generate UUID in model’s afterConstruct and assign it to parent record’s id (yes, my PKs are not integer).

When the files are being uploaded, I already know the id to attach to.

And the ‘main’ record is saved with that predefined id, of course.

So no matter how many forms user has opened, all they will be saved with different ids, and the uploaded files also will be saved with different parent ids.

Example (pseudocode):




$record = new MyModel; // here ID is already assigned in afterconstruct

echo $record->id; // result is UUID

if (is_uploaded_file(...)) {

    $image = new MyModelImage;

    $image->parentId = $record->id;

    $image->save();

}



I think you got the idea.

Pros:

  • you can display uploaded files even if record’s validation has failed

  • no need to deal with session (which is very unreliable)

  • almost bulletproof

Cons:

  • possible orphan files (daily cleanup required)

  • chance of collision (very small)

  • small degrade in performance (integer PKs are faster)