0

I need to download a file in SharePoint Online from PHP.

The only data I have is the URL of the folder.

Access is public, does not require authentication.

How can you complete this task from PHP? Any API?

Thanks in advance.

4
  • do you only have folder name or also file name ? Jun 5 '19 at 11:35
  • when you say access is public..is it for whole site, folder or only file ? Jun 5 '19 at 11:41
  • I have access to the folder, but I can infer the name of the file. Another alternative is to look for it because it is known that there is only one file. Access is public only to that folder. No to the site. Jun 5 '19 at 17:43
  • did you try below phpSPO ? Jun 6 '19 at 3:50
1

I struggled with the same issue using phpSPO, found a direct way to download. Simply replace everything after the ? in the url with "download=1" src

$fileUrl    = "https://mycompany.sharepoint.com/folders/someGUIDlikeSTRING?download=1";
$filePath   = "/tmp/saveHere.file";
$strCmd     = "wget -O  " . $filePath . " " . $fileUrl;
shell_exec($strCmd);

Curl kept receiving a 403, while wget worked fine. Its a hackish shell call, obviously curl would be a better solution, but it works.

1
2

You can use phpSPO library (https://github.com/vgrem/phpSPO). It has 2 methods which can be used. below is snippet from another post on stackoverflow

function downloadFile(ClientRuntimeContext $ctx, $fileUrl, $targetFilePath){
    $fileContent = 
Office365\PHP\Client\SharePoint\File::openBinary($ctx,$fileUrl);
    file_put_contents($targetFilePath, $fileContent);
    print "File {$fileUrl} has been downloaded successfully\r\n";
}

function downloadFileAsStream(ClientRuntimeContext $ctx, $fileUrl, 
    $targetFilePath) {
    $fileUrl = rawurlencode($fileUrl);

    $fp = fopen($targetFilePath, 'w+');
    $url = $ctx->getServiceRootUrl() . "web/getfilebyserverrelativeurl('$fileUrl')/\$value";
    $options = new \Office365\PHP\Client\Runtime\Utilities\RequestOptions($url);
    $options->StreamHandle = $fp;
    $ctx->executeQueryDirect($options);
    fclose($fp);

    print "File {$fileUrl} has been downloaded successfully\r\n";
}

Ref link

0

Here is the full source code to download SharePoint image. Library Url : https://github.com/vgrem/phpSPO

use Office365\Runtime\Auth\AuthenticationContext; use Office365\SharePoint\ClientContext; use Office365\Runtime\Auth\UserCredentials; use Office365\SharePoint\File as OfficeFile; use Drupal\file\Entity\File;

///////////////// CODE /////////////////////////

$username = 'Your SharePoint Email ID';
$password = 'Your SharePoint Password';
$fileUrl = "https://abc.sharepoint.com/abcxsed.abcx";
$fileUrl = end(explode("https://abc.sharepoint.com", $fileUrl));

$url = "https://abc.sharepoint.com/";
if(!empty($fileUrl)){
try {
  $credentials = new UserCredentials($username, $password);
  $ctx = (new ClientContext($url))->withCredentials($credentials);
  $fileContent = OfficeFile::openBinary($ctx,$fileUrl);
  $localFileName = end(explode("/", $fileUrl));
  $publicFilePath = \Drupal::service('file_system')->realpath(file_default_scheme() . "://");
  $localPath = "sites/deafults/files/property/".$property_unique_id.'_'.$localFileName;
  if (!is_writable($localPath)) {
    chmod($publicFilePath."/property", 0775);
  }
  $files = file_put_contents($localPath, $fileContent);
}

//catch exception
catch(Exception $e) {
  echo 'Authentication failed: ',  $e->getMessage(), "\n";
 }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.