6 Replies Latest reply on Sep 14, 2016 4:48 AM by robertwoods

    You CAN upload files to container fields with CWP and no plugins

    PowerSlave

      Hey guys (and gals),

      I've been looking for a way to upload files to container fields using PHP and I finally cracked it. After trolling through posts everywhere it seems that everyone had given up, assuming that the only way was to use SuperContainer.

      Not anymore.

      The big key to this technique is the InsertFromURL script step in FM12. Without this , it won't work, so sorry to all of you "Legacy" guys out there running < Version 12.

      I have done this on a Windows server only, but Mac should be the same.

      The first step is to create a folder on your website and add some PHP code to upload the file to that folder on your web server.:

      ______________________________________________________________________________________________________________________________________________________________________________________

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

      <?php

      require_once ('FileMaker.php');

       

       

      $target_path = "uploads/";// where the files will end up this path must be "writable" and "readable" by the web and it's relative to your site folder.

      $fname = basename( $_FILES['uploadedfile']['name']);// Set the filename

      $target_path = $target_path . $fname; // Set the destination for the file so PHP can move it

       

      if($fname != ''){

      // the file was sucessfully posted to the web server

      if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {

      // the file successfully moved from the PHP temp folder to the "uploads/" folder

       

       

      echo "The file ". $fname . " has been uploaded and moved to the right place";// the file move was successful message

      // ##########################################################################################################################################################################

      // now add the file to the database using the InsertFromURL technique (FM12 only)

      $fmupload = new FileMaker('databasename','database_server_address','username','password');

      // run the script that inserts a file into a container field

      $runscript=$fmupload->newPerformScriptCommand('database','layout',"http://127.0.0.1/the_path_to_the_copied_file/" . $fname); // build the object to run the FMServer script using the url of the moved file as the script parameter

      $runscriptresult=$runscript->execute(); // execute the script

      if (FileMaker::isError($runscriptresult)) { echo "There was a problem running the script" ; die($runscriptresult->getMessage()); } // check that the script ran ok and if not, show why

      }

      // ##########################################################################################################################################################################

      else{

       

      echo "There was an error uploading the file, please check your settings!";// the PHP engine could not move the file (check your permissions on the folders)

      }

      }

      ?>

      <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

      <head>

      <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

      <title>Add File to a FileMaker Container field using CWP/PHP</title>

      </head>

      <body>

      <form enctype="multipart/form-data" action="addFile.php" method="POST">

      Choose a file to upload into a container field: <input name="uploadedfile" type="file" /><br />

      <input type="submit" value="Upload File" />

      </form>

      </body>

      </html>

      _________________________________________________________________________________________________________________________________________________________________________________________

       

      So that's the PHP out of the way, the only thing left is the FM script :

       

      Goto Layout[ The layout that shows your container field ]

      New Record/Request

      Set Variable[$url ; Value:Get(ScriptParameter)]

      Insert from URL [Select, No Dialog ; database_name::ContainerField ; $url]

      Exit Script

       

      This script merely creates a new record , then inserts whatever file you uploaded through the web page into your container field using the new Insert From URL script step.

      __________________________________________________________________________________________________________________________________________________________________________________________

       

      The beauty of this is there are no plugins required !! None of this would work without the new InsertFromURL script step in FM12 AND the fact it can be executed server-side.

      If you wanted to you could add more variables to the script parameter if you needed to, just get the FileMaker script to parse them (things like user ID , and any other key fields that may be required to identify this file). This

      technique could be real handy for a web driven DMS (document management system) or media libraries , or anything else that utilizes FM container fields without the need for a FM client.

       

      Just remember to set permissions for the destination folder and make sure the destination folder is published to the web. For those who are security concious , at the end of the php script you can add a line to delete the file in the "uploads" folder once the file is inserted into the container field. This way your folder will be kept 'clean'. Also you can restrict the IP addresses that can access the "uploads" to just the server itself , (ie nobody except the server can see what's in the 'uploads' folder).

      This whole challenge has just opened up my eyes again to new possibilities of FileMaker just with this InsertFromURL script step.

      Enjoy!