Integrations and APIs for Service

Get Involved. Join the Conversation.

Topic

    Patricia Harvath
    Connect PHP and File Attachment... Please Help ... MY LAST...
    Topic posted May 3, 2013 by Patricia HarvathBronze Medal: 1,250+ Points 
    2351 Views, 44 Comments
    Title:
    Connect PHP and File Attachment... Please Help ... MY LAST DAY
    Content:

    Hi,

    This is my last day and I want to fix this before I go...

    Error when attaching file.

    'File path is not in temporary directory' 
    
    Where is the tmp file?
    

     

    THANKS!!

    Version:
    may 2012
    Code Snippet:

    Comment

    • Ryan McCullough

      Your script looks good. You should be able to reference /tmp/ from your PHP script. Does the file exist at '/tmp/'.$filename?

    • Patricia Harvath

      Nothing is in the tmp folder

    • Allan Schrum

      Take a look in the RDC Developer's Conference for 2011 Forum. The presentation on Connect PHP discusses this issue. Please see:


      http://communities.rightnow.com/posts/e29e156088

      From that presentation (does not cut-n-paste well):

      There are two ways to begin with adding a file attachment:
      Via the setFile() method:
      // Assumes that $tmpfnameis an existing file
      // in the “tmp” folder for the site:
      $fa->setFile( $tmpfname);


      Via the makeFile() method:
      // Gets a file resource for the script to write to:
      $fp= $fa->makeFile();
      fwrite( $fp, __FUNCTION__." writing to tempfile\n" );
      fclose( $fp);

      From there, wrap it up with:
      // Set the content type
      $fa->ContentType= 'text/plain';
      // Give it a name
      $fa->FileName= 'SomeName.suffix';
      // Append to the list
      $con->FileAttachments[] = $fa;
      $con->save();

      If you know where that file is located, then use that path. Otherwise you need to copy it into a file using the makeFile() process.

      Regards,

      -Allan

    • Allan Schrum

      I think some posts crossed.

      The fwrite() function will write the data to the temporary file created by makeFile(). If the content you wish to write is in the variable $content, then fwrite( $fp, $content ); will put that data into the temporary file so that it can be converted to the file attachment when you call "save()" on the top-level object (the one getting the file attachment). You can issue multiple fwrite() calls if desired. Please be careful if you are using large files because there is a limit to both memory and the size of the file that can be created.

      Regards,

      -Allan

    • Patricia Harvath

      Thanks Alan.  I don't have much experience with MAGIC constants...

      I got the file to upload and that worked great!

      What file extensions are supported?  We need to be able to upload excel spreadsheets and when I download it from the RN console and try to open it, I get an error that the file extension might not be valid.

       

      Here's my code that worked:

      if($fileName){
                          $idata->FileAttachments = new RNCPHP\FileAttachmentIncidentArray();
                          $fa = new RNCPHP\FileAttachmentIncident();
                          $fa->ContentType = "text/plain";
                          $fp = $fa->makeFile();
                          fwrite( $fp, __FUNCTION__ );
                          fclose( $fp );
                          $fa->ContentType = "text/plain";
                          $fa->FileName= $fileName;
                          $idata->FileAttachments[] = $fa;    
                      }

    • Patricia Harvath

      I just tried a PDF and a Word doc.  I got corrupt messages on these too.

      Any idea what could be causing that?

       

    • Allan Schrum

      The download process is essentially a web-browser download. The ContentType indicates that content and you are saying that it is "text/plain". The browser (or OS) uses that information to determine which program to invoke to open the document. Try setting it to "application/pdf" for your PDF document. A list of common ContentTypes can be found here: http://en.wikipedia.org/wiki/MIME_type .

      Regards,

      -Allan

    • Patricia Harvath

      $mime_types = array(
                  'txt' => 'text/plain',
                  'pdf' => 'application/pdf',
                  'doc' => 'application/msword',
                  'xls' => 'application/vnd.ms-excel');

      if($fileName){
                          $idata->FileAttachments = new RNCPHP\FileAttachmentIncidentArray();
                          $fa = new RNCPHP\FileAttachmentIncident();        

                          $fp = $fa->makeFile();
                          fwrite( $fp, __FUNCTION__ );
                          fclose( $fp );                
                          
                              $ext = strtolower(array_pop(explode('.',$fileName)));
                              if (array_key_exists($ext, $mime_types)) {
                                  $fa_mime = $mime_types[$ext];
                              }
                              elseif (function_exists('finfo_open')) {
                                  $finfo = finfo_open(FILEINFO_MIME);
                                  $mimetype = finfo_file($finfo, $fileName);
                                  finfo_close($finfo);
                                  $fa_mime = $mimetype;
                              }
                              else {
                                  $fa_mime = 'application/octet-stream';
                              }
                              
                          $fa->ContentType = $fa_mime;
                          $fa->FileName= $fileName;
                          $idata->FileAttachments[] = $fa;    
                      }

      So, I am trying to catch probable mime types.  But this doesn't work either.  In Chrome, the request payload shows as :

      ------WebKitFormBoundaryNDiZ8si3vmYoFn5t Content-Disposition: form-data; name="file"; filename="TEST.doc" Content-Type: application/msword

      and the Preview is:

      {"name":"TEST.doc","type":"application\/msword","tmp_name":"TeA1kD.custhelp.com","error":0,"size":19968}


      Thanks once again for your help!!!

       

       

    • Allan Schrum

      From email:

      What is the error that you are getting? If it is that the application does not like the content or the application not being launched? If it is because of the content, your test script is putting in __FUNCTION__ value in the file which might not be valid content for the application.

    • Patricia Harvath

      "Word cannot start the converter mswrd632.wpc"

      Adobe Reader could not open "test"_F634.pdf" because it is either not a supported file type or because the file has been damaged.

    • Allan Schrum

      From email:

      So the applications are now being launched. The content in your test script just is not good content for those applications. Can you replace the fwrite() of __FUNCTION__ with real file contents?

    • Patricia Harvath

      From my received email:

      Allan,
      
      You are the BEST!  Thanks for helping!!
      
      Ok, so what do you mean by 'real file contents?'
      
    • Allan Schrum

      From email:

      A real MS Word file, a real PDF document, etc.

      I'm sure if you put in plain text and called it text/plain it works fine (because the contents of the file align with the ContentType).

    • Patricia Harvath

      From my email archive:

                                                                  $fp = $fa->makeFile();
      
                                                                  $fp = "test.doc";
      
                                          //                      fwrite( $fp, __FUNCTION__ );
      
                                                                  fclose( $fp );
      
      Like that?  I get error:
      
       'RightNow\Connect\v1_2\ConnectAPIError' with message 'Minimum file size is 1 byte'
      
    • Allan Schrum

      From email:

      No. The $fp variable is the file pointer that is used by fwrite() to create the file. Do not change that variable once it is set.

      Where is the content of your file that you are trying to create? Your "test.doc" file exists somewhere, correct? Either you must identify where it is on the server (which is sort of where you started), or you must pass the contents of the file to your Connect PHP script so that you can fwrite( $fp, $yourContents ); to the file.

      You are close. But where is the content of the file you are trying to add?