Mustafa Turab Ali
Mustafa Turab Ali

Reputation: 25

Salesforce/php - downloaded image attachments are corrupted - Any Ideas?

Here is my code for downloading attachment from Salesforce.com using php toolkit and enterprise wsdl:

    header('Content-Type: application/force-download');
    header('Content-Disposition: inline; filename="image.jpg"');        
    $mySforceConnection = getConnection();
    $query = "SELECT Id, Name, Body from Attachment Where Id ='" .$id ."'";
    $queryResult = $mySforceConnection->query($query);

    $records = $queryResult->records;
    print_r(base64_decode($records[0]->fields->Body));

When I do this the file gets downloaded correctly with correct number of bytes but when I open the image, the windows image viewer says its corrupt. Any idea why this is happening?

The same code works fine for PDFs and text files.

Upvotes: 1

Views: 1451

Answers (1)

VictorKilo
VictorKilo

Reputation: 1870

You really want to just echo the output, as @eyescream mentioned. When you use the print_r function, additional tab and newline characters are placed into the output to make it more readable. A plain echo would output properly.

header('Content-Type: application/force-download');
header('Content-Disposition: inline; filename="image.jpg"');

$mySforceConnection = getConnection();

$query = "SELECT Id, Name, Body from Attachment Where Id ='" .$id ."'";
$queryResult = $mySforceConnection->query($query);

$records = $queryResult->records;
echo base64_decode($records[0]->fields->Body);

Upvotes: 1

Related Questions