Manaus Posted September 30, 2014 Share Posted September 30, 2014 Hi, I'm using this code to get the only file of a page. I uploaded the file using the admin pages. <? if ($page->name == "myproject3"): $myfile = "somefile.pdf"; $myfile = $page->documents->get('name='.$myfile); echo $myfile->url; But I get nothing, not even null... Thanks for any suggestion Link to comment Share on other sites More sharing options...
Ivan Gretsky Posted September 30, 2014 Share Posted September 30, 2014 (edited) I did not get into all this, but maybe: $myfile = $page->documents->get("name={$myfile}"); But why not limit "Maximum files allowed" and output it like this: <?php if($page->documents) { echo $page->documents->url; } ?> If you plan to have multiple files uploads, you should leave "Maximum files allowed" option as it is and do foreach. Edited September 30, 2014 by Ivan Gretsky 2 Link to comment Share on other sites More sharing options...
Nico Knoll Posted September 30, 2014 Share Posted September 30, 2014 <?php if ($page->name == "myproject3") { $myfile = "somefile.pdf"; // directly creating the path because you know where it lays and wht it's name is $myfileUrl = $config->urls->assets.$page->id.'/'.$myfile); echo $myfileUrl; } /* second option: using foreach */ if ($page->name == "myproject3") { $myfile = "somefile.pdf"; foreach($page->documents as $document) { if(basename($document->url) != $myfile) continue; $myfileUrl = $document->url; } echo $myfileUrl; } 3 Link to comment Share on other sites More sharing options...
adrian Posted October 1, 2014 Share Posted October 1, 2014 I actually can't see why your original code isn't working. I often use that approach: https://processwire.com/talk/topic/7700-find-image-by-image-name/ Link to comment Share on other sites More sharing options...
netcarver Posted October 1, 2014 Share Posted October 1, 2014 (edited) Hi, I'm using this code to get the only file of a page. I uploaded the file using the admin pages. <? if ($page->name == "myproject3"): $myfile = "somefile.pdf"; $myfile = $page->documents->get('name='.$myfile); echo $myfile->url; But I get nothing, not even null... Thanks for any suggestion First off, shouldn't the switch to PHP be "<?php" ?? After that, are you sure your code example is complete? I've never seen that form of if statement used without a switch back out of interpreted PHP straight after it. Have you cropped things out to simplify your example by any chance? After looking at alternative control structures that looks ok. The other thing to check is that your $myfile string is case-for-case the same as the filename that gets written into the name field as I think the comparison is case sensitive. Edited October 1, 2014 by netcarver 3 Link to comment Share on other sites More sharing options...
netcarver Posted October 1, 2014 Share Posted October 1, 2014 @Manaus Thank you for posting your code - I learnt something from it about PHP alternative control structures today! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now