Jump to content

Using External Files as Includes in Templates


Warran
 Share

Recommended Posts

Hi,

We have a web application and I am trying to use external php files as includes within templates. This is so we can centralise certain files that all instances of the web application commonly use.

I am trying to work around the setting allow_url_fopen=0 which the host does not allow to be changed. So, something like this, after checking if the file exists, doesn't work.

<?php

 $filename = "/some/external/path/external_file.php";
 include($filename);

Also tried with cURL to bypass allow_url_fopen=0.

Any help would be appreciated.

Thanks.


 

 

Link to comment
Share on other sites

52 minutes ago, Warran said:

We have a web application and I am trying to use external php files as includes within templates. This is so we can centralise certain files that all instances of the web application commonly use.

It sounds like you are looking for sharing code/functionality across multiple instances. That's what ProcessWire modules are for. 😉 You put the logic in one module and all instances can use it.

It's really just a few lines of code. Much simpler than in many other frameworks: https://processwire.com/docs/modules/development/

Link to comment
Share on other sites

  • 2 weeks later...

Thanks all

I've been on holiday. Sorry for not replying sooner.

cURL has worked now.

It didn't work until the external file was a text file. Then I used curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); to get the contents and used filePutContents and a save path with a new extension of .php.

<?php

    $externalFileURL = 'https://domain.com/files/filename.txt';

    $ch = curl_init($externalFileURL);
    curl_setopt($ch, CURLOPT_URL, $externalFileURL);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $fileContents = curl_exec($ch);
    curl_close($ch);
    
    $savePath = $config->paths->templates .  'filename.php';
    $files->filePutContents($savePath, $fileContents);
    include $savePath;

?>

Thanks again

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...