Can't add files programatically without User

Permalink
I have a custom form block for job applications, it allows the user to supply a CV (ms word document) along with other fields, then when submitted it adds that CV to a specific fileset.

This works when I am logged in as admin, but when I visit the site without being logged in I get the follwing error:

mysql error: [1048: Column 'uID' cannot be null] in EXECUTE("INSERT INTO FileSets(fsID,fsName,uID,fsType,fsOverrideGlobalPermissions) VALUES (NULL,'Job Application CVs',NULL,1,0)")


The code I'm using is as follows:

if( isset($_FILES['job_app_cv']) && is_uploaded_file($_FILES['job_app_cv']['tmp_name']) ) {
                $resp = $fi->import($_FILES['job_app_cv']['tmp_name'], $_FILES['job_app_cv']['name']);
                if (!($resp instanceof FileVersion)) {
                    switch($resp) {
                        case FileImporter::E_FILE_INVALID_EXTENSION:
                            $this->set('err', t('Invalid CV file extension.'));
                            break;
                        case FileImporter::E_FILE_INVALID:
                            $this->set('err', t('Invalid CV file.'));
                            break;
                    }
                    return;
                } else {
                    $_POST['job_app_cv_fid'] = $resp->getFileID();
                    $fs = new FileSet();


Can anybody help me spot what's wrong?

nickcardoso
 
JohntheFish replied on at Permalink Reply
JohntheFish
Solutions

1. Get your applicants to register as users before completing the form. This will be better for security and reduce spam submissions. (This is what I would do)

2. Submit the files under the identity of an existing user. For security, best not to use the super admin, so maybe create a user called 'Auto Recruiter' with minimal permissions and use that uID for creating the files. You may need to get a bit dirty inside the file imported to do that. (This is not what I would do, but its not my site)
nickcardoso replied on at Permalink Reply
nickcardoso
Hey, thanks,

I actually did try logging in a user (the admin) but the following code didn't help either (and is bound to be exceptionally bad practice, so I'm not sure how it should be done - it was just an attempt to see if having a user worked)

//########
$logout = false;
if (empty($u)) {
    $u = User::getByUserID(1); //load admin user
    User::loginByUserID(1);
    $logout = true;
}
//########
$_POST['job_app_photo_fid'] = $resp->getFileID();
$fs = new FileSet();
$fs = $fs->createAndGetSet('Job Application Photos', FileSet::TYPE_PUBLIC);
//getByName('Job Application Photos');
$fs->addFileToSet($resp);
//########
if ($logout) {