-1

I did create a function in file test.php. When i open the file directly, the function is working. But when i include the file in the wordpress header I get following error:

Fatal error: Uncaught Error: Call to a member function getDirectories() on null in /home/vdouser/domains/mytriplef.nl/public_html/test.php:48 Stack trace: #0 /home/vdouser/domains/mytriplef.nl/public_html/test.php(227): addFolders() #1 /home/vdouser/domains/mytriplef.nl/public_html/wp-content/themes/mytriplef/header.php(3): require_once('/home/vdouser/d...') #2 /home/vdouser/domains/mytriplef.nl/public_html/wp-includes/template.php(730): require_once('/home/vdouser/d...') #3 /home/vdouser/domains/mytriplef.nl/public_html/wp-includes/template.php(676): load_template('/home/vdouser/d...', true, Array) #4 /home/vdouser/domains/mytriplef.nl/public_html/wp-includes/general-template.php(48): locate_template(Array, true, true, Array) #5 /home/vdouser/domains/mytriplef.nl/public_html/wp-content/themes/mytriplef/front-page.php(1): get_header() #6 /home/vdouser/domains/mytriplef.nl/public_html/wp-includes/template-loader.php(106): include('/home/vdouser/d...') #7 /home/vdouser/domains/mytriplef.nl/public_html/wp-blog-header.php(19) in /home/vdouser/domains/mytriplef.nl/public_html/test.php on line 48

/public_html/wp-content/themes/mytriplef/header.php code:

<?php

require_once("test.php");

/public_html/test.php code:

<?php

require($_SERVER['DOCUMENT_ROOT'] . '/wp-load.php');


ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$servername = "localhost";
$username = "x";
$password = "x";
$database = "x";

// Create connection
$conn = new mysqli($servername, $username, $password, $database);

// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

require_once "vendor/autoload.php"; 
use MicrosoftAzure\Storage\File\FileRestProxy;

$accountName = "x";
$accountKey = "x";

$shareName = "uploadmytriplef/MyTripleF/Inkomend";

$connectionString = "DefaultEndpointsProtocol=https;AccountName=$accountName;AccountKey=$accountKey";
$fileClient = FileRestProxy::createFileService($connectionString);
$sas = 'st=2020-09-22T08%3A45%3A26Z&se=2088-07-16T08%3A45%3A00Z&sp=rl&sv=2018-03-28&sr=s&sig=ANWMpYimZlvEM4qDVuDpljy%2B4KNHT%2BC47qtvrsDlXno%3D';
$list = $fileClient->listDirectoriesAndFiles($shareName);

$azureFoldersArray = array();

function addFolders() {

    global $shareName;
    global $list;
    global $fileClient;
    global $accountName;
    global $sas;
    global $azureFoldersArray;

    // FOLDERS
    foreach($list->getDirectories() as $folders) {

        $folderName = $folders->getName();
        $shareName = "uploadmytriplef/MyTripleF/Inkomend/".$folderName."";
        $list = $fileClient->listDirectoriesAndFiles($shareName);

        // CREATE FOLDER
        $shareName = "uploadmytriplef/MyTripleF/Inkomend/$folderName";
        if (!file_exists("azure_storage/".$folderName."")) {
            mkdir("azure_storage/".$folderName."", 0777, true);
        }
        
        array_push($azureFoldersArray, $folderName);

        addFiles($folderName, $shareName);

        global $conn;

        $sql = "INSERT INTO mytriplef_folders (name) VALUES ('$folderName')";

        if ($conn->query($sql) === TRUE) {
            
        } else {
            
        }

    };

    checkFolders();

}

Does someone know why function is not working when i include it in header.php but it works when i directly visit the test.php file

Thanks!

Johannes
  • 64,305
  • 18
  • 73
  • 130
Tring Bing
  • 17
  • 1
  • **Warning:** You are wide open to [SQL Injections](https://stackoverflow.com/a/60496/1839439) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Oct 01 '20 at 14:04

1 Answers1

0

Put the test.php file into the same directory as the file which includes it, i.e. into[...]..../mytriplef/, which is obviously your theme folder. that way the filepath you use (i.e. including it without any directories) will be correct.

Johannes
  • 64,305
  • 18
  • 73
  • 130