0

Should I be able to use PHP Curl to upload a file to Sharepoint. I have this logic that I am using at the moment to "try" and upload to "My Shared Document" for now.

$the_file = file_get_contents('index.html');

$user = '&&&&&&&';
$pass = '&&&&&&&';

$ch = curl_init();

curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: multipart/form-data"));
curl_setopt($ch, CURLOPT_USERPWD, "$user:$pass");
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Linux i686; rv:6.0) Gecko/20100101 Firefox/6.0Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_URL, 'https://&&&&.net/personal/&&&&&&&/Shared%20Documents/Forms/AllItems.aspx');

curl_setopt($ch, CURLOPT_POST, true);

$post = array('file_contents' => "the_file");

curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

$response = curl_exec($ch);

if(curl_errno($ch)) {
    echo json_encode('Error: ' . curl_error($ch));
}
else {
    echo json_encode($response);
}

Any help is much appreciated.

Thanks

Martin

1
  • Is your issue got resolved? Commented Dec 13, 2023 at 17:19

1 Answer 1

1

When you upload a file through curl, few things you need to consider.

  1. use @ with file path to upload the file directly. example:

    $post = array('file_contents' => "@/home/xyz/the_file");
    // if you have parameter with the file, then it will be,
    $post = array('uid' => 110, 'file_contents' => "@/home/xyz/the_file");


2. You don't need to specify this header "Content-type: multipart/form-data". It will be added automatically when you use @ with your file parameter.

Sign up to request clarification or add additional context in comments.

1 Comment

I tried this solution but upon receiving the positive response from CURL the file is not there in the SharePoint mentioned location. any clue?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.