Q:

upload the document to notes and attachments using php toolkit salesforce

I am trying to do the integration using PHP Toolkit and creating a record in salesforce (Custom Object).I am having an issue
Example :Let's say i have a candidate applying for the job .The Applicant needs to upload the resume and save as (Notes and attachments) with the candidate record.
Can Anyone help me on this requirement.

Code:
<html>
<head>
<link href="style.css" rel="stylesheet" type="text/css" media="screen" />
<title> Creating candidate using PHP Integration for HR Application
</title>
</head>
<body>
<?php
define("TOKEN_URL", "https://cs8.salesforce.com/services/oauth2/token");
define("USERNAME", "XXXXXXXXXXXXXXXXXXXXXXX");
define("PASSWORD", "XXXXXXXXXXXXXXXXXXXXXXXXX");
define("SECURITY_TOKEN", "XXXXXXXXXXXXXXXXXXXXXXXXXX");
require_once ('soapclient/SforcePartnerClient.php');
$mySforceConnection = new SforcePartnerClient();
$mySforceConnection->createConnection("Partner.xml");
$mySforceConnection->login(USERNAME, PASSWORD.SECURITY_TOKEN);

$params = "grant_type=password&client_id=" . CLIENT_ID
        . "&client_secret=" . CLIENT_SECRET . "&username=" . USERNAME
        . "&password=" . PASSWORD . SECURITY_TOKEN;

        //Get Access token
        $ch = curl_init(TOKEN_URL);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);

        $json_response = curl_exec($ch);

        $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);

        $response = json_decode($json_response, true);
        $access_token = $response['access_token'];
        $instance_url = $response['instance_url'];
?>
<div id="wrapper">
    <div id="page">
        <div id="page-bgtop">
            <div id="page-bgbtm">
                <div id="content">
                    <div class="post">
                        <div style="clear: both;">&nbsp;</div>
                        <div class="entry">
                                <a href="javascript:void(0);">                          
                                </a>                            
                                <?php

                                if(isset($_POST['submit']))
                                    {
                                try {
                                    $fields = array (
                                                    'First_Name__c' => $_POST['Firstname'],
                                                    'Last_Name__c' => $_POST['Lastname'],
                                                    'Primary_contact_number__c'=>$_POST['primContactNumber'],
                                                    'Secondary_contact_number__c'=>$_POST['seoContactnumber'],
                                                    'Email__c'=> $_POST['Email'],
                                                    'How_did_you_learn_of_this_Opportunity__c'=>$_POST['HdylotOpp'],
                                                    'Please_Specify__c'=>$_POST['PleaseSpecify'],
                                                    'Status__c'=>$_POST['Status'],
                                                    'Have_you_worked_with_us_before__c'=>$_POST['Workbefore'],
                                                    'When__c'=>$_POST['When'],
                                                    'Available_to_start__c'=>$_POST['AvlToStart'],                                              
                                                    'Street_Address_1__c'=>$_POST['StreetAddress1'],
                                                    'Street_Address_2__c'=>$_POST['StreetAddress2'],
                                                    'Province__c'=>$_POST['Province'],
                                                    'City__c'=>$_POST['City'],
                                                    'Country__c'=>$_POST['Country'],
                                                    'Postal_Code__c'=>$_POST['ZIP'],
                                                    'Currently_Employed__c'=>intval($_POST['CurrentlyEmployed']),
                                                    'Current_Employer__c'=>$_POST['CurrentEmployer'],
                                                    'Start_Date_Work_Exp1__c'=>$_POST['StartDate'],
                                                    'End_Date_Work_EXP_End1__c'=>$_POST['EndDate'],
                                                    'Rate_of_Pay_1__c'=>$_POST['RateofPay'],
                                                    'Previous_Employer__c'=>$_POST['PreviousEmployer'],
                                                    'Start_Date_Prev_Work_1__c'=>$_POST['StartDate1'],
                                                    'End_Date_Prev_Wor_END_1__c'=>$_POST['EndDate1'],
                                                    'Rate_of_Pay_2__c'=>$_POST['RateofPay1'],
                                                    'Reason_for_Leaving__c'=>$_POST['ReasonforLeaving'],
                                                    'Previous_Employer_1__c'=>$_POST['PreviousEmployer1'],
                                                    'Start_Date_Prev_Work_2__c'=>$_POST['StartDate2'],
                                                    'End_Date_Prev_Wor_END_2__c'=>$_POST['EndDate2'],
                                                    'Rate_of_Pay_3__c'=>$_POST['RateofPay2'],
                                                    'Reason_for_Leaving_1__c'=>$_POST['ReasonforLeaving2'],
                                                    'Higher_Education__c'=>$_POST['HigherEducation'],
                                                    'Licenses_Certfications__c'=>$_POST['LicensesCertifications'],
                                                    'Reference_Name_1__c'=>$_POST['ReferenceName'],
                                                    'Company__c'=>$_POST['Company'],
                                                    'Relationship__c'=>$_POST['Relationship'],
                                                    'Phone_Number_1__c'=>$_POST['Phone'],
                                                    'Email_Ref_1__c'=>$_POST['Email'],
                                                    'Reference_Name_2__c'=>$_POST['ReferenceName1'],
                                                    'Company_1__c'=>$_POST['Company1'],
                                                    'Relationship_1__c'=>$_POST['Relationship1'],
                                                    'Phone_Number_2__c'=>$_POST['Phone1'],
                                                    'Email_Ref_2__c'=>$_POST['Email1'],
                                                    'Reference_Name_3__c'=>$_POST['ReferenceName2'],
                                                    'Company_3__c'=>$_POST['Company2'],
                                                    'Relationship_2__c'=>$_POST['Relationship2'],
                                                    'Phone_Number_3__c'=>$_POST['Phone2'],
                                                    'Email_Ref_3__c'=>$_POST['Email2'],

                                                    );
                                          $sObject = new SObject();
                                          $sObject->fields = $fields;
                                          $sObject->type = 'Candidate__c';
                                          echo "**** Creating the following:\r\n";
                                          $createResponse = $mySforceConnection->create(array($sObject));
                                          print_r($createResponse);
                                          } catch (Exception $e) {
                                                echo $mySforceConnection->getLastRequest();
                                                echo $e->faultstring;
                                                }

                                                //Get the record id from last insert call and use that to make attachment call
        $recordid = $createResponse[0]->id;

        $url = "$instance_url/services/data/v20.0/sobjects/Attachment/";

        //Upload attachment
        if(isset($_FILES['uploaddoc']) && $_FILES['uploaddoc']!=null)
        {
            $fileName = $_FILES['uploaddoc']['name'];
            $tmpName  = $_FILES['uploaddoc']['tmp_name'];
            $fileSize = $_FILES['uploaddoc']['size'];
            $fileType = $_FILES['uploaddoc']['type'];

            if($fileSize>0)
            {
                $fp      = fopen($tmpName, 'r');
                $content = fread($fp, filesize($tmpName));
                $fileData = base64_encode($content);
                $data = array("ParentId" => "$recordid", "Name" => 'resume_' . date('Y-m-d H:i:s') . '.pdf', "body" => "$fileData");
                $data_string = json_encode($data);
                $curl = curl_init($url);
                curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($curl, CURLOPT_HEADER, false);
                curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($curl, CURLOPT_HTTPHEADER,
                array("Authorization: OAuth $access_token","Content-type: application/json"));
                curl_setopt($curl, CURLOPT_POST, true);
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);

                $json_response = curl_exec($curl);

                $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

                if ( $status != 201 ) {
                    die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
                }

                curl_close($curl);

                $response = json_decode($json_response, true);

                $attachmentid = $response["id"];
            }

        }

                                }       
                                ?>
                        </div>
                    </div>
                    <div style="clear: both;">&nbsp;</div>
                </div>
                <!-- end #content -->

                <!-- end #sidebar -->
                <div style="clear: both;">&nbsp;</div>
            </div>
        </div>
    </div>
    <!-- end #page -->
</div>
<!-- end #footer -->
</body>
</html>

Form:
    <html>
<form action="CandidateCreationPage.php" method="post" enctype="multipart/form-data">
<Section><b> Candidate Information </b></section></br></br>
FirstName: <input type="text" name="Firstname" ></br></br>
Last Name: <input type="text" name="Lastname" ></br></br>
Primary contact number :<input type="phone" name="primContactNumber" ></br></br>
Secondary contact number :<input type="phone" name="seoContactnumber" ></br></br>
Email: <input type="Email" name="Email" ></br></br>
How did you learn of this Opportunity: <select name="HdylotOpp">
                                        <option value="NONE">--NONE--</option>
                                        <option value="Kijiji">Kijiji</option>
                                        <option value="Craigslist">Craigslist</option>
                                         <option value="Indeed">Indeed</option>
                                        <option value="Workopolis">Workopolis</option>
                                         <option value="Linkedin">Linkedin</option>
                                        <option value="Trailerwizards.com">Trailerwizards.com</option>
                                         <option value="Current Employee/ Friend">Current Employee/ Friend</option>
                                        <option value="Local newspaper">Local newspaper</option>
                                        <option value="Other">Other</option>
                                        </select></br></br>
Please Specify (If Applicable): <input type="text" name="PleaseSpecify" ></br></br>
Do you want to work: <select name="Status">
                                        <option value="NONE">--NONE--</option>
                                        <option value="Full-time">Full-time</option>
                                        <option value="Part-time">Part-time</option>
                                         <option value="Summer Student/ Co-op">Summer Student/ Co-op</option>
                                        <option value="Contract">Contract</option>
                                         <option value="On Call">On Call</option>
                                        </select></br></br>
Have you worked with us before :<select name="Workbefore">
                                        <option value="Yes">Yes</option>
                                        <option value="No">No</option>
                                        </select></br></br>
When (If Applicable) :<input type="date" name="When" ></br></br>                                            
Avaliable to start :<input type="date" name="AvlToStart" ></br></br>

<Section><b> Address Information </b></section></br></br>                                   
Street Address 1 : <input type="text" name="StreetAddress1"></br></br>
Street Address 2 : <input type="text" name="StreetAddress2"></br></br>
State/province :<input type="Text" name="Province"></br></br>
City: <input type="text" name="City"></br></br>
Country :<input type="text" name="Country"></br></br>
Postal Code:<input type="text" name="ZIP"></br></br>

<Section><b> Work Experience </b></section></br></br>
Currently Employed? : <input type="checkbox" name="CurrentlyEmployed"></br></br>
Current Employer : <input type="text" name="CurrentEmployer"></br></br>
Start Date: <input type="date" name="StartDate"></br></br>
End Date : <input type="date" name="EndDate"></br></br>
Rate of Pay: <input type="number" min="0" max="1000000" name="RateofPay"></br></br>

Previous Employer : <input type="text" name="PreviousEmployer"></br></br>
Start Date: <input type="date" name="StartDate1"></br></br>
End Date : <input type="date" name="EndDate1"></br></br>
Rate of Pay: <input type="number" min="0" max="1000000" name="RateofPay1"></br></br>
Reason for Leaving :<textarea type="Text"  name="ReasonforLeaving"></textarea></br></br>

Previous Employer : <input type="text" name="PreviousEmployer1"></br></br>
Start Date: <input type="date" name="StartDate2"></br></br>
End Date : <input type="date" name="EndDate2"></br></br>
Rate of Pay: <input type="number" min="0" max="1000000" name="RateofPay2"></br></br>
Reason for Leaving :<textarea type="Text"  name="ReasonforLeaving2"></textarea></br></br>
<Section><b> Education /Qualifications</b></section></br></br>
Higher Education: <select name="HigherEducation">
                                        <option value="NONE">--NONE--</option>
                                        <option value="University Degree">University Degree</option>
                                        <option value="Post-Grad Degree">Post-Grad Degree</option>
                                         <option value="High School">High School</option>
                                        <option value="College Diploma">College Diploma</option>
                                         <option value="High School Diploma">High School Diploma</option>
                                        </select></br></br>
Licenses & Certifications: <select name="LicensesCertifications">
                                        <option value="NONE">--NONE--</option>
                                        <option value="Forklift License">Forklift License</option>
                                        <option value="Technician License">Technician License</option>
                                         <option value="Apprentice">Apprentice</option>
                                        <option value="Valid Driver's License">Valid Driver's License</option>
                                         <option value="First Aid Training">First Aid Training</option>
                                        </select></br></br>
<Section><b> References </b></section></br></br>

Reference Name :<input type="text" name="ReferenceName"></br></br>
Company :<input type="text" name="Company"></br></br>
Relationship :<input type="text" name="Relationship"></br></br>
Phone :<input type="phone" name="Phone"></br></br>
Email :<input type="Email" name="Email"></br></br>

Reference Name :<input type="text" name="ReferenceName1"></br></br>
Company :<input type="text" name="Company1"></br></br>
Relationship :<input type="text" name="Relationship1"></br></br>
Phone :<input type="phone" name="Phone1"></br></br>
Email :<input type="Email" name="Email1"></br></br>

Reference Name :<input type="text" name="ReferenceName2"></br></br>
Company :<input type="text" name="Company2"></br></br>
Relationship :<input type="text" name="Relationship2"></br></br>
Phone :<input type="phone" name="Phone2"></br></br>
Email :<input type="Email" name="Email2"></br></br>

Resume: <input id="breachpolicy" name="uploaddoc" class="input-file" type="file">

<input type="submit" name="submit" value="submit"/>

</form>
</html>

A:

You need to create one app inside your salesforce as shown below

You will get consumer secret and consumer key out of it which you will use in your code.
Add following line inside your form

add following php code will insert attachment. I have added code from your previous question.
        define("CLIENT_ID", "YOURCONSUMERKEY");
        define("CLIENT_SECRET","CLIENT SECRET HERE");
        define("USERNAME", "YOUR USERNAME HERE");
        define("PASSWORD", "YOUR PASSWORD HERE");
        define("SECURITY_TOKEN", "TOKEN HERE");

        define("TOKEN_URL", "https://YOURINSTANCEURL/services/oauth2/token")

        $params = "grant_type=password&client_id=" . CLIENT_ID
        . "&client_secret=" . CLIENT_SECRET . "&username=" . USERNAME
        . "&password=" . PASSWORD . SECURITY_TOKEN;

        //Get Access token
        $ch = curl_init(TOKEN_URL);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);

        $json_response = curl_exec($ch);

        $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);

        $response = json_decode($json_response, true);
        $access_token = $response['access_token'];
        $instance_url = $response['instance_url'];

        $fields = array ('First_Name__c' =>$_POST['Firstname'],'Last_Name__c' =>$_POST['Lastname'],'Mobile__c' => $_POST['Mobile']);
        $sObject = new SObject();
        $sObject->fields = $fields;
        $sObject->type = 'Candidate__c';
        echo "**** Creating the following:\r\n";
        $createResponse = $mySforceConnection->create(array($sObject));
        //Get the record id from last insert call and use that to make attachment call
        $recordid = $createResponse[0]->id;

        $url = "$instance_url/services/data/v20.0/sobjects/Attachment/";

        //Upload attachment
        if(isset($_FILES['uploaddoc']) && $_FILES['uploaddoc']!=null)
        {
            $fileName = $_FILES['uploaddoc']['name'];
            $tmpName  = $_FILES['uploaddoc']['tmp_name'];
            $fileSize = $_FILES['uploaddoc']['size'];
            $fileType = $_FILES['uploaddoc']['type'];

            if($fileSize>0)
            {
                $fp      = fopen($tmpName, 'r');
                $content = fread($fp, filesize($tmpName));
                $fileData = base64_encode($content);
                $data = array("ParentId" => "$recordid", "Name" => 'resume_' . date('Y-m-d H:i:s') . '.pdf', "body" => "$fileData");
                $data_string = json_encode($data);
                $curl = curl_init($url);
                curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($curl, CURLOPT_HEADER, false);
                curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($curl, CURLOPT_HTTPHEADER,
                array("Authorization: OAuth $access_token","Content-type: application/json"));
                curl_setopt($curl, CURLOPT_POST, true);
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);

                $json_response = curl_exec($curl);

                $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

                if ( $status != 201 ) {
                    die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
                }

                curl_close($curl);

                $response = json_decode($json_response, true);

                $attachmentid = $response["id"];
            }

        }

