2

I want to upload files into the document library. Is it possible to do this with the PHP Toolkit?

1 Answer 1

2

It is possible to use the PHP toolkit to upload Document records. The 'trick' is to pass the base64-encoded file content in the Document's Body field. Here is some sample code...

HTML page with file upload element:

<!DOCTYPE html>
<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
    Select file to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload" name="submit">
</form>

</body>
</html>

PHP to receive file and send to Salesforce as a Document (save as upload.php):

<html>
    <head>
    </head>
    <body>
<?php
// This example uses the Enterprise client, since Document is a standard object
require_once ('soapclient/SforceEnterpriseClient.php');

// Edit these! Ideally, read them from a config file or the environment
// rather than setting username, password etc in source
define("SF_SECURITY_TOKEN", "YOUR_SECURITY_TOKEN");
define("SF_USERNAME", "[email protected]");
define("SF_PASSWORD", "p455w0rd");

// Max size for a Document in Salesforce is 5MB
$maxDocSize = 5 * 1024 * 1024;

// Do we have a file?
if ($_FILES['fileToUpload']['error'] == UPLOAD_ERR_OK           //checks for errors
    && is_uploaded_file($_FILES['fileToUpload']['tmp_name'])    //checks that file is uploaded
    && $_FILES['fileToUpload']['size'] <= $maxDocSize) {        //checks file size

    // Login to Salesforce
    $mySforceConnection = new SforceEnterpriseClient();
    $mySforceConnection->createConnection("soapclient/enterprise.wsdl.xml");
    $mySforceConnection->login(SF_USERNAME, SF_PASSWORD.SF_SECURITY_TOKEN);

    // Create Document record
    $document = new stdclass();
    $document->Name = $_FILES['fileToUpload']['name'];
    $document->FolderId = '00l500000019lSHAAY'; // CHANGE THIS TO YOUR FOLDER ID!!!
    $document->Description = 'Upload of '.$_FILES['fileToUpload']['name'];
    $document->Body = base64_encode(file_get_contents($_FILES['fileToUpload']['tmp_name']));

    $response = $mySforceConnection->create(array($document), 'Document');

    // Did it work?
    if (isset($response[0]->success) && $response[0]->success) {
        echo 'Success - created Document with Id '.$response[0]->id;
    } else {
        echo 'Error: '.$response[0]->errors[0]->message;
    }
} else {
    // Some error uploading the file
    if (count($_FILES) === 0) {
        echo 'Error: no file, or file was too big - check post_max_size in php.ini';
    } else if ($_FILES['fileToUpload']['size'] > $maxDocSize) {
        echo 'Error: file is bigger than Document limit of 5MB';
    } else {
        $phpFileUploadErrors = array(
            0 => 'There is no error, the file uploaded with success',
            1 => 'The uploaded file exceeds the upload_max_filesize directive in php.ini',
            2 => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form',
            3 => 'The uploaded file was only partially uploaded',
            4 => 'No file was uploaded',
            6 => 'Missing a temporary folder',
            7 => 'Failed to write file to disk.',
            8 => 'A PHP extension stopped the file upload.',
        );

        echo 'Error: '.$phpFileUploadErrors[$_FILES['fileToUpload']['error']];
    }
}
?>
    </body>
</html>

The maximum size for a Document in Salesforce is 5MB. Since the default maximum file upload size in PHP is 2MB, you may need to edit php.ini and set upload_max_filesize to 5M.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .