[英]Function is working when visiting php file but when including php file it is not working
我確實在文件test.php
創建了一個函數。 當我直接打開文件時,該功能正在運行。 但是當我將文件包含在 wordpress 標題中時,我收到以下錯誤:
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
代碼:
<?php
require_once("test.php");
/public_html/test.php
代碼:
<?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();
}
有人知道為什么當我將它包含在header.php
時函數不起作用,但是當我直接訪問test.php
文件時它起作用
謝謝!
將test.php
文件放入與包含它的文件相同的目錄中,即放入[...]..../mytriplef/
,這顯然是您的主題文件夾。 這樣你使用的文件路徑(即包括它沒有任何目錄)將是正確的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.