The SPFile
class handles all the file operations in SharePoint.
Gets all the SharePoint Files from a SharePoint Folder
<?php
require 'vendor/autoload.php';
use Http\Adapter\Guzzle6\Client as HttpClient;
use Http\Message\MessageFactory\GuzzleMessageFactory as MessageFactory;
use Impensavel\Spoil\Exception\SPRuntimeException;
use Impensavel\Spoil\SPFile;
use Impensavel\Spoil\SPFolder;
use Impensavel\Spoil\SPList;
use Impensavel\Spoil\SPSite;
try {
// SharePoint Site configuration
$config = [
// ...
];
// Instantiate SharePoint Site
$site = new SPSite('https://example.sharepoint.com/sites/mySite/', $config, new HttpClient, new MessageFactory);
// Generate Access Token
$site->createSPAccessToken();
// Get a Folder by relative URL (option #1)
$folder = SPFolder::getByRelativeUrl($site, 'myFolder');
// Get a List by title (option #2)
$folder = SPList::getByTitle($site, 'My List');
// Get all the Files from the Folder/List we just got
$files = SPFile::getAll($folder);
// Do something with the files
foreach ($files as $file) {
var_dump($file->toArray());
}
} catch (SPRuntimeException $e) {
// Handle exceptions
}
Gets a SharePoint File by its relative URL
<?php
require 'vendor/autoload.php';
use Http\Adapter\Guzzle6\Client as HttpClient;
use Http\Message\MessageFactory\GuzzleMessageFactory as MessageFactory;
use Impensavel\Spoil\Exception\SPRuntimeException;
use Impensavel\Spoil\SPFile;
use Impensavel\Spoil\SPSite;
try {
// SharePoint Site configuration
$config = [
// ...
];
// Instantiate SharePoint Site
$site = new SPSite('https://example.sharepoint.com/sites/mySite/', $config, new HttpClient, new MessageFactory);
// Generate Access Token
$site->createSPAccessToken();
$file = SPFile::getByRelativeUrl($site, 'myFolder/mySubfolder/image.png');
} catch (SPRuntimeException $e) {
// Handle exceptions
}
Gets a SharePoint File by its name
<?php
require 'vendor/autoload.php';
use Http\Adapter\Guzzle6\Client as HttpClient;
use Http\Message\MessageFactory\GuzzleMessageFactory as MessageFactory;
use Impensavel\Spoil\Exception\SPRuntimeException;
use Impensavel\Spoil\SPFile;
use Impensavel\Spoil\SPFolder;
use Impensavel\Spoil\SPList;
use Impensavel\Spoil\SPSite;
try {
// SharePoint Site configuration
$config = [
// ...
];
// Instantiate SharePoint Site
$site = new SPSite('https://example.sharepoint.com/sites/mySite/', $config, new HttpClient, new MessageFactory);
// Generate Access Token
$site->createSPAccessToken();
// Get a Folder by relative URL (option #1)
$folder = SPFolder::getByRelativeUrl($site, 'myFolder/mySubfolder');
// Get a List by title (option #2)
$folder = SPList::getByTitle($site, 'My List');
$file = SPFile::getByName($folder, 'image.png');
} catch (SPRuntimeException $e) {
// Handle exceptions
}
Create a SharePoint File
<?php
require 'vendor/autoload.php';
use Http\Adapter\Guzzle6\Client as HttpClient;
use Http\Message\MessageFactory\GuzzleMessageFactory as MessageFactory;
use Impensavel\Spoil\Exception\SPRuntimeException;
use Impensavel\Spoil\SPFile;
use Impensavel\Spoil\SPFolder;
use Impensavel\Spoil\SPList;
use Impensavel\Spoil\SPSite;
try {
// SharePoint Site configuration
$config = [
// ...
];
// Instantiate SharePoint Site
$site = new SPSite('https://example.sharepoint.com/sites/mySite/', $config, new HttpClient, new MessageFactory);
// Generate Access Token and Form Digest
$site->createSPAccessToken()->createSPContextInfo();
// Get a Folder by relative URL (option #1)
$folder = SPFolder::getByRelativeUrl($site, 'myFolder/mySubfolder');
// Get a List by title (option #2)
$folder = SPList::getByTitle($site, 'My List');
// Content from an SplFileInfo object
$content = new SplFileInfo('document.pdf');
// If null, the file name from the SplFileInfo will be used
$name = null;
// Content from a resource
$content = fopen('document.pdf', 'r');
// An SPBadMethodCallException will be thrown if the name is not provided
$name = 'document.pdf';
// Content from a string
$content = 'Document content...';
// An SPRuntimeException will be thrown if the name is not provided
$name = 'document.pdf';
// Allow overwriting the file if it already exists
// An SPBadMethodCallException will be thrown if the file exists and overwriting wasn't allowed
$overwrite = false;
$file = SPFile::create($folder, $content, $name, $overwrite);
} catch (SPRuntimeException $e) {
// Handle exceptions
}
Update a SharePoint File
<?php
require 'vendor/autoload.php';
use Http\Adapter\Guzzle6\Client as HttpClient;
use Http\Message\MessageFactory\GuzzleMessageFactory as MessageFactory;
use Impensavel\Spoil\Exception\SPRuntimeException;
use Impensavel\Spoil\SPFile;
use Impensavel\Spoil\SPFolder;
use Impensavel\Spoil\SPList;
use Impensavel\Spoil\SPSite;
try {
// SharePoint Site configuration
$config = [
// ...
];
// Instantiate SharePoint Site
$site = new SPSite('https://example.sharepoint.com/sites/mySite/', $config, new HttpClient, new MessageFactory);
// Generate Access Token and Form Digest
$site->createSPAccessToken()->createSPContextInfo();
// Get a Folder by relative URL (option #1)
$folder = SPFolder::getByRelativeUrl($site, 'myFolder/mySubfolder');
// Get a List by title (option #2)
$folder = SPList::getByTitle($site, 'My List');
$file = SPFile::getByName($folder, 'document.pdf');
// Content from an SplFileInfo object
$content = new SplFileInfo('document2.pdf');
// Content from a resource
$content = fopen('document2.pdf', 'r');
// Content from a string
$content = 'New document content...';
$file = $file->update($content);
} catch (SPRuntimeException $e) {
// Handle exceptions
}
Move and/or rename a SharePoint File.
<?php
require 'vendor/autoload.php';
use Http\Adapter\Guzzle6\Client as HttpClient;
use Http\Message\MessageFactory\GuzzleMessageFactory as MessageFactory;
use Impensavel\Spoil\Exception\SPRuntimeException;
use Impensavel\Spoil\SPFile;
use Impensavel\Spoil\SPFolder;
use Impensavel\Spoil\SPList;
use Impensavel\Spoil\SPSite;
try {
// SharePoint Site configuration
$config = [
// ...
];
// Instantiate SharePoint Site
$site = new SPSite('https://example.sharepoint.com/sites/mySite/', $config, new HttpClient, new MessageFactory);
// Generate Access Token and Form Digest
$site->createSPAccessToken()->createSPContextInfo();
// Get a Folder by relative URL (option #1)
$folder1 = SPFolder::getByRelativeUrl($site, 'myFolder/mySubfolder');
// Get another Folder by relative URL (option #1)
$folder2 = SPFolder::getByRelativeUrl($site, 'otherFolder');
// Get a List by title (option #2)
$folder1 = SPList::getByTitle($site, 'My List');
// Get another List by title (option #2)
$folder2 = SPList::getByTitle($site, 'My Other List');
// Get the File we want to move
$file = SPFile::getByName($folder1, 'document.pdf');
// Rename the file (If null, the original name will be used)
$name = 'moved_document.pdf';
$file->move($folder2, $name);
} catch (SPRuntimeException $e) {
// Handle exceptions
}
Copy a SharePoint File
<?php
require 'vendor/autoload.php';
use Http\Adapter\Guzzle6\Client as HttpClient;
use Http\Message\MessageFactory\GuzzleMessageFactory as MessageFactory;
use Impensavel\Spoil\Exception\SPRuntimeException;
use Impensavel\Spoil\SPFile;
use Impensavel\Spoil\SPFolder;
use Impensavel\Spoil\SPList;
use Impensavel\Spoil\SPSite;
try {
// SharePoint Site configuration
$config = [
// ...
];
// Instantiate SharePoint Site
$site = new SPSite('https://example.sharepoint.com/sites/mySite/', $config, new HttpClient, new MessageFactory);
// Generate Access Token and Form Digest
$site->createSPAccessToken()->createSPContextInfo();
// Get a Folder by relative URL (option #1)
$folder1 = SPFolder::getByRelativeUrl($site, 'myFolder/mySubfolder');
// Get another Folder by relative URL (option #1)
$folder2 = SPFolder::getByRelativeUrl($site, 'otherFolder');
// Get a List by title (option #2)
$folder1 = SPList::getByTitle($site, 'My List');
// Get another List by title (option #2)
$folder2 = SPList::getByTitle($site, 'My Other List');
// Get the File we want to copy
$file = SPFile::getByName($folder1, 'document.pdf');
// Rename the file (If null, the original name will be used)
$name = 'copied_document.pdf';
// Allow overwriting the file if it already exists
// An SPRuntimeException will be thrown if the file exists and overwriting wasn't allowed
$overwrite = false;
$file->copy($folder2, $name, $overwrite);
} catch (SPRuntimeException $e) {
// Handle exceptions
}
Delete a SharePoint File
<?php
require 'vendor/autoload.php';
use Http\Adapter\Guzzle6\Client as HttpClient;
use Http\Message\MessageFactory\GuzzleMessageFactory as MessageFactory;
use Impensavel\Spoil\Exception\SPRuntimeException;
use Impensavel\Spoil\SPFile;
use Impensavel\Spoil\SPFolder;
use Impensavel\Spoil\SPList;
use Impensavel\Spoil\SPSite;
try {
// SharePoint Site configuration
$config = [
// ...
];
// Instantiate SharePoint Site
$site = new SPSite('https://example.sharepoint.com/sites/mySite/', $config, new HttpClient, new MessageFactory);
// Generate Access Token and Form Digest
$site->createSPAccessToken()->createSPContextInfo();
// Get a Folder by relative URL (option #1)
$folder = SPFolder::getByRelativeUrl($site, 'myFolder/mySubfolder');
// Get a List by title (option #2)
$folder = SPList::getByTitle($site, 'My List');
// Get the File we want to delete
$file = SPFile::getByName($folder, 'document.pdf');
$file->delete();
} catch (SPRuntimeException $e) {
// Handle exceptions
}
Retrieve an array
representation of the SPFile
object.
Example:
var_dump($file->toArray());
Output:
array(11) {
["sp_type"]=>
string(18) "SP.Data.mySubfolderItem"
["id"]=>
int(123)
["guid"]=>
string(36) "00000000-0000-ffff-0000-000000000000"
["title"]=>
NULL
["name"]=>
string(12) "document.pdf"
["size"]=>
string(5) "65536"
["created"]=>
object(Carbon\Carbon)#55 (3) {
["date"]=>
string(26) "2000-01-01 00:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(13) "Europe/London"
}
["modified"]=>
object(Carbon\Carbon)#59 (3) {
["date"]=>
string(26) "2000-01-01 00:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(13) "Europe/London"
}
["relative_url"]=>
string(31) "/sites/mySite/myFolder/mySubfolder/document.pdf"
["author"]=>
string(55) "i:0i.t|membership|username@example.onmicrosoft.com"
["extra"]=>
array(0) {
}
}
Get the SPFile
id.
Example:
echo $file->getID();
Output:
123
Get the SPFile
name.
Example:
echo $file->getName();
Output:
document.pdf
Get the SPFile
size in kilobytes.
Example:
echo $file->getSize();
Output:
65536
Get the SPFile
relative URL.
Example:
echo $file->getRelativeUrl();
Output:
/sites/mySite/myFolder/mySubfolder/document.pdf
Get the SPFile
URL.
Example:
echo $file->getUrl();
Output:
https://example.sharepoint.com/sites/mySite/myFolder/mySubfolder/document.pdf
Get the SPFile
author.
Output:
echo $file->getAuthor();
Output:
i:0i.t|membership|username@example.onmicrosoft.com
TIP: The SPUser class can be used to get more info about the author.
Get the contents of the SPFile
.
file_put_contents('document.pdf', $file->getContents());
This method is similar to the toArray()
one, with the exception that it includes the url
and excludes the sp_type
, title
, relative_url
, author
and extra
values.
Example:
var_dump($file->getMetadata());
Output:
array(11) {
["id"]=>
int(123)
["guid"]=>
string(36) "00000000-0000-ffff-0000-000000000000"
["name"]=>
string(12) "document.pdf"
["size"]=>
string(5) "65536"
["created"]=>
object(Carbon\Carbon)#55 (3) {
["date"]=>
string(26) "2000-01-01 00:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(13) "Europe/London"
}
["modified"]=>
object(Carbon\Carbon)#59 (3) {
["date"]=>
string(26) "2000-01-01 00:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(13) "Europe/London"
}
["url"]=>
string(78) "https://example.sharepoint.com/sites/mySite/myFolder/mySubfolder/document.pdf"
}
Get the associated SharePoint Item of a SPFile
. This method is normally used when the metadata of a SPFile
needs to be set.
try {
$item = $file->getSPItem();
$item->update([
'Title' => 'A PDF Document',
// Custom fields
'CustomField1' => 'Foo',
]);
} catch (SPRuntimeException $e) {
// Handle exceptions
}
SPFile
property methods belong to a trait and are documented here.
SPFile
timestamp methods belong to a trait and are documented here.