How to access/read files/pages on webside - php

in my case I get this error massage
Fatal error: Uncaught Error: Class "RandomClass" not found in /webspace/projectfolder/pages/add_eventuser.php:10 Stack trace: #0 {main} thrown in /webspace/projectfolder/pages/add_eventuser.php on line 10
I use
$_SERVER['DOCUMENT_ROOT']
to get the exact path of the file
In my structure i organized the files like this:
projectfolder/includes/class.php where the pointed class should loaded
projectfolder/pages/initiatorpage.php where i got the Fatal Error
public class DatabaseColums
{
public function loadDatabase($databaseName)
{
$databaseName = "test";
return $databaseName;
}
}
include $_SERVER['DOCUMENT_ROOT'].'includes/RandomClass.php';
$dbinput = new DatabaseColums();
echo $dbinput->loadDatabase();
I made the folder/file restrictions read/write/executeable
Cant explain to myself why I got still this massage.
What Issue is behind that.
Thank you for your attention

the problem was a missing / between $_SERVER['DOCUMENT_ROOT'] and /folder/file.php
--right :$_SERVER['DOCUMENT_ROOT']./ folder/file.php
--wrong: $_SERVER['DOCUMENT_ROOT'].folder/file.php
Thank you #Ajmal Praveen and
#ADyson

Related

MongoDB insertOne function not found

I'm trying to use MONGODB in my project and I just started using it when it has this problem. I created another directory and ran the files with no problems whatsoever. What is wrong with this? I keep getting Fatal error: Uncaught Error: Call to undefined method MongoDB\Database::insertOne() error message.
This is my code to the class called Database which is producing the error.
<?php
namespace auth;
include_once dirname(__DIR__) . "/config.php";
class Database
{
public function __construct(
private readonly string $dbname,
){
$this->run();
}
private function run(): void
{
$collection = (new \MongoDB\Client)->{$this->dbname};
$insertOneResult = $collection->insertOne([
'username' => 'admin',
'email' => 'admin#example.com',
'name' => 'Admin User',
]);
printf("Inserted %d document(s)\n", $insertOneResult->getInsertedCount());
var_dump($insertOneResult->getInsertedId());
}
}
and my test.php file just contained some lines of code
include_once "./assets/php/config.php";
$database = new \auth\Database("test->users");
I definitely loaded the class MongoDB and my config.php includes the vendor file of composer's.
This is the error in full.
Fatal error: Uncaught Error: Call to undefined method MongoDB\Database::insertOne() in C:\xampp\htdocs\PP\assets\php\Classes\Database.php:18
Stack trace:
#0 C:\xampp\htdocs\PP\assets\php\Classes\Database.php(11): auth\Database->run()
#1 C:\xampp\htdocs\PP\test.php(15): auth\Database->__construct('test->users')
#2 {main}
thrown in C:\xampp\htdocs\PP\assets\php\Classes\Database.php on line 18
PHP Fatal error: Uncaught Error: Call to undefined method MongoDB\Database::insertOne() in C:\xampp\htdocs\PPa\assets\php\Classes\Database.php:18
Stack trace:
#0 C:\xampp\htdocs\PP\assets\php\Classes\Database.php(11): auth\Database->run()
#1 C:\xampp\htdocs\PP\test.php(15): auth\Database->__construct('test->users')
#2 {main}
thrown in C:\xampp\htdocs\PP\assets\php\Classes\Database.php on line 18
Process finished with exit code 255
The config.phpfile that I have included
include_once dirname(__DIR__, 2) . "/vendor/autoload.php";
include_once "autoload.php";
Thanks for any help.
Ah yes answer to my question, I'm just well not thinking properly at the time of writing the code. The `MongoDB\Collection" class accepts two arguments from the user and one internally set. The user provided argument is the database name and collection name. Doing it with (new \MongoDB\Client)->{$this->dbname} is wrong because it provides just the database string and not the collection name.
I resolved this issue by changing how the structure looks of the collection. md is just the alias for MongoDB class as I do not want to type such a long string everytime.
$collection = (new md\Client)->$db->$collection_name;
However, I have no idea how mongoDB uses the db and collection_name as magic constants as I have looked through the entire MongoDB\Client file and found there is no code stating those "magic constants"? I would appreciate if someone has an answer to this.

Fatal error: Uncaught Error: Call to undefined function that is not in a class

So i am working on some code and have come across this error and its starting to bug me a little as i cant find out what is wrong
i have 2 files main.php and that includes functions.php now in the main file i have the following code
<?php
include "functions.php";
$NAME = GET_USER_NAME();
?>
That is all thats in the main file now in the functions file i have the following code
<?php
function GET_USER_NAME() {
return 'bob';
}
function GET_USER_AGE() {
return '5';
}
?>
now when i try and open main in my browser i get the error
Fatal error: Uncaught Error: Call to undefined function GET_USER_NAME() in /var/www/html/main.php:3 Stack trace: #0 {main} thrown in /var/www/html/main.php on line 3
i know the file is being included properly as i can run the function
GET_USER_AGE();
from the main file and it returns 5
The only possible solutions to your problem are :
A typo in the function called in the main, which is not the exact same as in your included file (case sensitivity ?)
The include references a file which is not the one you want (but has one of the 2 desired functions)
And also yes, as #Markus-Zeller says, don't use method names like that, as stated here : https://www.php-fig.org/psr/psr-1/#1-overview

iLovePDF Library - Fatal error: Class 'Ilovepdf' not found

I was trying to compress PDF. I try my best to find out some library that can be free too for some limited purpose. I found out iLovePDF Library.
I try to get it done using composer and without composer too But not find any way to resolve it.
My Code:
<?php
require_once('vendor/autoload.php');
// require_once('vendor/ilovepdf/init.php');
$ilovepdf = new Ilovepdf('project_public_key','secret_key');
$myTask = $ilovepdf->newTask('compress');
$file1 = $myTask->addFile('file1.pdf');
$myTask->execute();
$myTask->download();
?>
Fatal error: Uncaught Error: Class 'Ilovepdf' not found in C:\xampp\htdocs\PHP Doc\E15\index.php:11 Stack trace: #0 {main} thrown in C:\xampp\htdocs\PHP Doc\E15\index.php on line 11
You need to use namespaces:
$ilovepdf = new \Ilovepdf\Ilovepdf('project_public_key','secret_key');
Or:
use \Ilovepdf\Ilovepdf;
$ilovepdf = new Ilovepdf('project_public_key','secret_key');
Thank You.
I resolved it by adding below line at the top of index.php
namespace Ilovepdf;

php - Class not found (composer.json)

I'm trying to implement a PHP Library for detecting CMS I found on github, but I can't for the life of me figure it out.
I am new to php and composer, but I installed composer in my directory, then updated the json as instructed, but it cannot find the classes, despite them all being in the directory.
running this code
include(__DIR__ . "/vendor/autoload.php");
$domain = "http://google.com";
$cms = new DetectCMS($domain);
if($cms->getResult()) {
echo "Detected CMS: ".$cms->getResult();
} else {
echo "CMS couldn't be detected";
}
gives me this error
PHP Fatal error: Uncaught Error: Class 'DetectCMS' not found in D:\Projects\Scraper\ccc.php:12
Stack trace:
#0 {main}
thrown in D:\Projects\Scraper\ccc.php on line 12
here is my file structure:
would anyone have any idea what I'm doing wrong with this information?
Thanks in advance
I think to include a class using the autoloader,in your class, you must do something about it:
in your composer.json
"autoload": {
"psr-4" :{"DetectCMS": "DetectCMS"}
}
after in your class DetectCMS.php:
<?php
use DetectCMS\DetectCMS;
or
<?php
$loader = require 'vendor/autoload.php';
$loader->add('DetectCMS', __DIR__.'DetectCMS');

Call to undefined function mod_dir()

I am trying to host an app which is already written and i am unable to process beyond this point, I know this might be very little detail to solve this issue. But anyone has any idea what this function is? (mod_dir()). Am i missing a library.
<?php
global $dir;
echo 'a';
$dir = mod_dir();
?>
Actual error : Fatal error: Call to undefined function mod_dir() in /Users/****/Sites/test/index.php on line 10

Categories