Get url of file stored in page attribute

Permalink 1 user found helpful
Hi all,

I have some code in a 5.7 site, I'm trying to get the url of a PDF that my client will add via a page attribute:

$pdfLink = $c->getAttribute('pdf_download');
$pdfLinkUrl = View::url('/download_file', $pdfLink, $c->getCollectionID());


Then later down the page, I have a print statement:

<?php print $pdfLinkUrl ?>


But this results in the error:
Object of class Concrete\Core\File\File could not be converted to string

How can I fix this please?

Thanks in advance
Dave

madesimplemedia
View Replies: View Best Answer
MrKDilkington replied on at Permalink Best Answer Reply
MrKDilkington
Hi madesimplemedia,

I believe the issue is $pdfLink in your example is a File object and not a string.

An alternative using URL:to()
$pdfFileObject = $c->getAttribute('pdf_download');
if (is_object($pdfFileObject)) {
    $pdfDownloadURL = URL::to('/download_file', $pdfFileObject->getFileID(), $c->getCollectionID());
}
// Example output:
//http://localhost/concrete5/index.php/download_file/46/196...

An alternative using the File method getDownloadURL()
- this does the same thing as the above approach, with less code
$pdfFileObject = $c->getAttribute('pdf_download');
if (is_object($pdfFileObject)) {
    $pdfDownloadURL = $pdfFileObject->getDownloadURL();
}
// Example output:
//http://localhost/concrete5/index.php/download_file/46/196...
madesimplemedia replied on at Permalink Reply
madesimplemedia
Spot on, thanks mate! :)