Salesforce’s web-to-lead form does not natively support uploading attachments. However, many businesses nowadays might need to collect a file or two with the lead information.
One easy hack is that when a customer submits the form, you upload the attachment to the web server, generate a link for it and store the this link to your lead object records. Then you can manually upload the file from the web server to Salesforce, if you need to, or store the file at the server for some time. For this example we are using PHP.
The steps to achieve this are the following:
- create a field for storing the attachment for lead object
- generate a web-to-lead form
- edit your web-to-lead form
- post the form information to Salesforce’s servers with PHP and curl
Create a field for storing the attachement
First you need to create a new field in lead object, where to store the file. Go to Object Manager and Lead object. Choose Fields & Relationships. Give your field a name and Save. Pay attention to the name of the field.

Generate a web-to-lead form
Go to your web-to-lead settings in Salesforce and create a new web-to-lead form. Copy the HTML and paste it to your favourite text editor.
You should have something similar in there:
<!-- NOTE: Please add the following <META> element to your page <HEAD>.-->
<!-- If necessary, please modify the charset parameter to specify the -->
<!-- character set of your HTML page. -->
<!-- ---------------------------------------------------------------------- -->
<META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"> <!-- ---------------------------------------------------------------------- -->
<!-- NOTE: Please add the following <FORM> element to your page. -->
<!-- ---------------------------------------------------------------------- --> <form action="https://webto.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8" method="POST">
<input type=hidden name="oid" value="YOUR_ORIGINAL_ID_HERE">
<input type=hidden name="retURL" value="YOUR_RETURN_URL_HERE">
<label for="first_name">First Name</label>
<input id="first_name" maxlength="40" name="first_name" size="20" type="text" />
<br>
<label for="last_name">Last Name</label>
<input id="last_name" maxlength="80" name="last_name" size="20" type="text" />
<br>
<label for="email">Email</label>
<input id="email" maxlength="80" name="email" size="20" type="text" /><br>
<label for="company">Company</label>
<input id="company" maxlength="40" name="company" size="20" type="text" />
<br>
<label for="city">Kaupunki</label>
<input id="city" maxlength="40" name="city" size="20" type="text" />
<br>
<label for="state">State</label> <input id="state" maxlength="20" name="state" size="20" type="text" />
<br>
<input type="submit" name="submit">
</form>
You can collect the file from the web-to-lead form. You just need to make two things first:
Update the form tag to enctype and change the form action to post it to your own server, E.g. fileuploadtoserver.php.
<form action="fileuploadtoserver.php" method="POST" enctype="multipart/form-data">
Add the file input field somewhere after other fields.
<input id="fileToUpload" name="fileToUpload" type="file">
Create a file called fileuploadtoserver.php and copy and paste the following there:
<?php
$url= "https://webto.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8";
$target_dir = "/var/www/html/yoursite.com/uploads/files/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$post_fields_str = "";
//pick up the values from the form
$post_fields = array();
foreach ($_POST as $key => $value){
$post_fields[$key] = urlencode($value);
}
// check, if we had a file
if(empty($_FILES["fileToUpload"]["name"])) {
$upload = 0;
}else{
$upload = 1;
}
$target = $target_dir . $md5name . basename($_FILES["fileToUpload"]["name"]);
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target)) {
$m = "Upload succeeded for ". basename( $_FILES["fileToUpload"]["name"]);
} else {
$error = $_FILES['fileToUpload']['error'];
$m = "Sorry, error: " . $error;
}
// make new link to the file
$publicUrlToFile = str_replace("/var/www/html/","https://www.yourserver.com/",$target_file);
// this is the field in the Salesforce, check your naming
if($upload==1){
$post_fields['Attachment__c'] = $publicUrlToFile;
}
//Check to see if cURL is installed ...
if (!function_exists('curl_init')){
die('Sorry cURL is not installed!');
}
// urlify
foreach($post_fields as $key=>$value) { $post_fields_str .= $key.'='.$value.'&'; }
rtrim($post_fields_str, '&');
//open connection
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($post_fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $post_fields_str);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
//execute post
$result = curl_exec($ch);
// http result
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// here you can make a redirect to returl
?>
You are done. Now try your form. You should have a attachment on your lead record.

Closing thoughts
Remember to check your server’s security settings. You might want to tighten your folder permissions. Anyone who has the address to your attachment file, can access your attachment. You can of course add more security with .htaccess. You can use md5 hashing for file names too. That way, the file names are too hard to guess and usually not easily reachable via url. Also make a cron script to remove old attachments, that are no longer needed on the web server.
If you are using WordPress’ Brilliant Web To Lead plugin, good news! You can upload files in minutes. I’ll tell you in my next post how. Stay tuned!
11 replies on “Salesforce hack: web-to-lead with file upload”
Any tips for collecting Salesforce leads with file attachments in WordPress?
Yes, coming up within few days.
Where do I create the php file in salesforce??
Hi Monica! You only create a field in Salesforce, where you store the physical address of the attachment file. The PHP file you create to your website (E.g. http://www.yoursite.com/yourphpfile.php), that is, if your website is using PHP. If you are using WordPress, there’s a little trick you can do with plugin called Brilliant Web-to-Lead for Salesforce. I was planning to make a separate post for that.
If my site is created in Digital Experiences, where should I save the php file? Or it doesn’t work in Digital Experiences?
File is not being uploaded into my WordPress server. I have given Home/web/mysite.com/public_html/wp-content/uploads/files/
this path as target_dir
Check that you have write permissions to your given folder. You need to use chmod command for this unless your hosting provider has graphical interface for this.
What if I have to do it with Shopify plateform ???
https://apps.shopify.com/salesforce-sync There’s an integration for Shopify. Use it. I’m not sure, if Shopify has free solution for receiving data, maybe you need to have Shopify API.
Can we use this in web-to-case ?
It doesn’t seem to post fields from multiselect checkboxes.
Only the Last value of the multiselect checkbox is sent to salesforce.
Is there a way around this?