phpSPO icon indicating copy to clipboard operation
phpSPO copied to clipboard

Can't upload pictures to lists.

Open zoltanbogar opened this issue 8 years ago • 1 comments

Greetings! I try to upload pictures to a specific list(SP2013) via php(5.6+). I use ntlm connect, because the other one throws "partner DNS..." error. I can see lists, and list items but not able to upload pictures, however the code does not throw any error.

I (kinda) use the following code to upload: $fileCreationInformation = new \Office365\PHP\Client\SharePoint\FileCreationInformation(); $fileCreationInformation->Content = file_get_contents($filename); $fileCreationInformation->Url = basename($filename); $uploadFile = $targetList->getRootFolder()->getFiles()->add($fileCreationInformation); $ctx->executeQuery(); print "File {$uploadFile->getProperty('Name')} has been uploaded\r\n";

The output is: "File has been uploaded". I'm wondering what am I doing wrong, I hope you can help. Thanks in advance!

zoltanbogar avatar Oct 03 '17 13:10 zoltanbogar

@zoltanbogar Use a try-catch to catch any exceptions. In case you are using a SharePoint List I suggest to use AttachmentCreationInformation instead of FileCreationInformation. For example:

$attCreationInformation = new \Office365\PHP\Client\SharePoint\AttachmentCreationInformation();
$attCreationInformation->FileName = basename($absoluteFilePath);
$attCreationInformation->ContentStream = file_get_contents($filename);
$attachment = $item->getAttachmentFiles()->add($attCreationInformation);
$ctx->executeQuery();
$attachmentURL = $attachment->getServerRelativeUrl():
$ctx->executeQuery();
print "Attachment has been uploaded to $attachmentURL\r\n";

Note that $item is the SharePoint ListItem you want to attach your file to.

mwe-code avatar Oct 06 '20 18:10 mwe-code