function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
makotoShishiomakotoShishio 

PHP Upload To Attachment

Hi,

 

I'am a complete newbie to salesforce in fact just got it today.

 

my question is does the PHP toolkit allow file uploads, if so

could somebody show a sample code

 

Thanks in advanced.

 

Park Walker (TAGL)Park Walker (TAGL)

The toolkit does not handle the file upload. You take care of that using the standard tools in PHP. The toolkit will allow you to create an Attachment object and set the Name, Body and ContentType. You'll need to call base64_encode() on the uploaded data before populating the Body field.

 

The API documentation has the details of the Attachment object but the only code example I am aware of is in the Force.com Cookbook and is in Java. 

 

Park

makotoShishiomakotoShishio

Thank you Park,

 

i will try you suggestions and read more into the attachment object

Mikoto.ShinkuMikoto.Shinku

Hi makoto ,there is a sample that maybe help u

 

upload.php

 

<?php
define("SOAP_CLIENT_BASEDIR", "../soapclient");
require_once (SOAP_CLIENT_BASEDIR.'/SforceEnterpriseClient.php');
require_once (SOAP_CLIENT_BASEDIR.'/SforceHeaderOptions.php');

$login = 'your Salesforce Id';
$password = 'your Salesforce pwd';
try {
// login
$mySforceConnection = new SforceEnterpriseClient();
$mySoapClient = $mySforceConnection->createConnection(SOAP_CLIENT_BASEDIR.'/enterprise.wsdl.xml');
$mylogin = $mySforceConnection->login($login, $password);

$response = $mySforceConnection->describeGlobal();

} catch (Exception $e) {
echo $mySforceConnection->getLastRequest();
echo $e->faultstring;
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>File upload</title>
</head>
<body>
<form action="uploadAttachment.php" method="post" enctype="multipart/form-data">
File<br>
SObject's Name<br>
<select name="select">
<?php
foreach ($response->sobjects as $record) {
if($record->searchable == 1){
?>
<option value="<?php print_r($record->name) ; ?>"><?php print_r($record->label) ; ?></option>
<?php
}else{
continue;
}
}?>
</select>
<input type="text" name="parentId"><br>
<input type="submit" value="Upload"> <input type="reset" name="reset">
</form>
</body>
</html>

uploadAttachment.php

 

<?php
define("SOAP_CLIENT_BASEDIR", "../soapclient");
require_once (SOAP_CLIENT_BASEDIR.'/SforceEnterpriseClient.php');
require_once (SOAP_CLIENT_BASEDIR.'/SforceHeaderOptions.php');

$login = 'your Salesforce Id';
$password = 'your Salesforce pwd';
try {
// login
$mySforceConnection = new SforceEnterpriseClient();
$mySoapClient = $mySforceConnection->createConnection(SOAP_CLIENT_BASEDIR.'/enterprise.wsdl.xml');
$mylogin = $mySforceConnection->login($login, $password);

// Search
$searchKey = $_POST["parentId"];
$tableKey = $_POST["select"];
$query = 'SELECT Id from ' . $tableKey . ' where Name = \'' . $searchKey . '\'';
print_r($query);
$response = $mySforceConnection->query(($query));

// helpful method
$file = $_FILES["attachment"]["tmp_name"];
$fileName = $_FILES["attachment"]["name"];
$parentID = $response->records[0]->Id;

$handle = fopen($file,'rb');
$file_content = fread($handle,filesize($file));
fclose($handle);
// encode
$encoded = chunk_split(base64_encode($file_content));

// the target Sobject
$sObject = new stdclass();
$sObject->Name = $fileName;
$sObject->ParentId = $parentID;
$sObject->body = $encoded;

// do upload
$createResponse = $mySforceConnection->create(array($sObject), 'Attachment');
var_dump($createResponse);
//print_r($createResponse->Id);
} catch (Exception $e) {
echo $mySforceConnection->getLastRequest();
echo $e->faultstring;
}

?>

Mikoto.Shinku

 

 

lokeguptalokegupta

Hi,

 I have also same problem of uploading files. I tried your example but it returns this error message -

 

SOAP-ERROR: Encoding: object hasn't 'type' property

 

Please help me with this issue. Or if you have any working code please provide that as well.

 

 

 

Thanks

Park Walker (TAGL)Park Walker (TAGL)

Try modifying this:

  // the target Sobject
  $sObject = new stdclass();
  $sObject->Name = $fileName;
  $sObject->ParentId = $parentID; 
  $sObject->body = $encoded;
  
  // do upload
  $createResponse = $mySforceConnection->create(array($sObject), 'Attachment');

 to this:

  // the target Sobject
  $sObject = new stdclass();
  $sObject->type = 'Attachment';
  $sObject->Name = $fileName;
  $sObject->ParentId = $parentID; 
  $sObject->body = $encoded;
  
  // do upload
  $createResponse = $mySforceConnection->create(array($sObject));

 

Park

chirag indylogixchirag indylogix
I have created upload file but there is no upload field for upload any file?
pablo Martinez 4pablo Martinez 4
Just an update on the code, for partner api:
// the target Sobject
$sObject = new stdclass();
$sObject->type = 'Attachment';
$sObject->fields['Name'] = $fileName;
$sObject->fields['ParentId'] = $parentID;
$sObject->fields['body'] = $encoded;
// do upload
$createResponse = $mySforceConnection->create(array($sObject));