Cannot upload files from frontend

Permalink 1 user found helpful
Hi guys
I'm building a custom package which needs to upload files from frontend. I cannot use Front end file uploader or any other file block. need to custom code it as per the requirement.
Here is my code:
Loader::library('file/importer');
            $fi = new FileImporter();
            $resp = $fi->import($_FILES['projDocuments']['tmp_name'], '', '');
            if (!($resp instanceof FileVersion)) {
               switch($resp) {
                  case FileImporter::E_FILE_INVALID_EXTENSION:
                     $this->error->add(t('Invalid file extension'));
                     break;
                  case FileImporter::E_FILE_INVALID:
                     $this->error->add(t('Invalid file.'));
                     break;
               }
            } else {
               $projDocuments = $resp->getFileID();
               Loader::model('file_set');


It doesn't upload the file. Am I doing anything wrong? Please help me. Its urgent!

Rony

ronyDdeveloper
 
exchangecore replied on at Permalink Best Answer Reply
exchangecore
Is there some reason you are sending empty strings as the 2nd and 3rd parameter of the import function? It looks like these default to false if you don't want to leverage them, and I'm not sure why you would want to name a file an empty string or if that might be causing you problems? Can you verify that you're getting expected results in your $resp variable right after the import, maybe try a var dump and die there to see what you've got?

http://www.concrete5.org/documentation/developers/files/importers/...

https://github.com/concrete5/concrete5/blob/123948b89480ceaabbcfb2c0...
ronyDdeveloper replied on at Permalink Reply
ronyDdeveloper
Thank you so much. Its very helpful. Somewhere I read that if I don't want to rename the file or its a fresh upload, not overwriting any existing files, then simply leave the other 2 parameters blank. So I was not sure to should be false rather blank. Thank you so much.

Rony
exchangecore replied on at Permalink Reply
exchangecore
Ah most likely that whatever you read actually meant to completely omit the 2nd and 3rd parameters, as opposed to setting them to be an empty string. You should be able to simply pass in the first parameter like so:

$resp = $fi->import($_FILES['projDocuments']['tmp_name']);


Glad you got it sorted out!
melat0nin replied on at Permalink Reply
melat0nin
Would you be willing to share your code, or an outline of how you achieved this?