PhpSpreadheet BaseWriter not found in PhpSpreadsheet/Writer/Xlsx,php:32 - php

I was using phpexcel on an old site (that doesn't even have composer), but i got stuck in this error.
Fatal error: Uncaught Error: Class 'PhpOffice\PhpSpreadsheet\Writer\BaseWriter' not found in /cpl/plugins/PhpOffice/PhpSpreadsheet/Writer/Xlsx.php:32 Stack trace: #0 /cpl/json/json.exportar.php(32): require_once() #1 {main} thrown in /cpl/plugins/PhpOffice/PhpSpreadsheet/Writer/Xlsx.php on line 32
Heres my code:
require_once '../plugins/PhpOffice/PhpSpreadsheet/Spreadsheet.php';
require_once '../plugins/PhpOffice/PhpSpreadsheet/Writer/Xlsx.php';
use \PhpOffice\PhpSpreadsheet\Spreadsheet;
use \PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World !');
$writer = new Xlsx($spreadsheet);
$writer->save('hello world.xlsx');
Before that, I tried to use an autoload, instead of the require_once:
<?php
function loadLibraries($class) {
$path = __DIR__."/plugins/";
require_once $path.$class.".php";
}
spl_autoload_register("loadLibraries");
And put it into my code:
require "../autoload.php";
use \PhpOffice\PhpSpreadsheet\Spreadsheet;
use \PhpOffice\PhpSpreadsheet\Writer\Xlsx;
But the thing is, then I had this error:
Fatal error: require_once(): Failed opening required '/cpl/plugins/PhpOffice\PhpSpreadsheet\Spreadsheet.php' (include_path=' ') in /cpl/autoload.php on line 6
I don't know why, but instead of plugins/PhpOffice/PhpSpreadsheet/... it's '\' and not '\'.
Then, I decided to manually require the classes, and got stuck on that error I first mentioned, I even tried to require
require_once '../plugins/PhpOffice/PhpSpreadsheet/Writer/BaseWriter.php';
use \PhpOffice\PhpSpreadsheet\Writer\BaseWriter;
But it didn't worked either.
Any ideas?

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.

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;

Can't find the source of the error on index.php using slim framework

I'm working on a slim api using and I have reinstalled the project several times and recreated the everything. I'm getting error on line 15 of my index.php file. this is the error
Fatal error: Uncaught RuntimeException: Unexpected data in output buffer
Notice: Undefined variable: db in /var/www/html/slimquote/index.php on line 15
Fatal error: Uncaught RuntimeException: Unexpected data in output buffer. Maybe you have characters before an opening <?php tag? in /var/www/html/slimquote/vendor/slim/slim/Slim/App.php:625 Stack trace: #0 /var/www/html/slimquote/vendor/slim/slim/Slim/App.php(335): Slim\App->finalize(Object(Slim\Http\Response)) #1 /var/www/html/slimquote/index.php(22): Slim\App->run() #2 {main} thrown in /var/www/html/slimquote/vendor/slim/slim/Slim/App.php on line 625
I have removed and search for any character before the opening tag Still I keep getting this error. does anyone know what this is?.. I have search all over, and everyone points to closing tags ?>, but I have none left. I have also commented out any line including database configuration and anything instantiating a database... any help appreciated.
index.php file
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
require 'vendor/autoload.php';
//require 'dbHandlers/DBconnection.php';
//$conn = new DBconnection();
//$db = $conn->dbConnect();
$app = new \Slim\App;
$app->get('/', function (Request $request, Response $response, array $args) {
// $query = 'SELECT * FROM quotes_tb ORDER BY id';
// $sql = mysqli_query($db, $query);
$response->getBody()->write("Hello world");
return $response;
});
$app->run();

use of class with namespace error

in api.php i use severall classes, with autoloaders. in include them like this
use \protoware\cms\Account as Account;
use \protoware\cms\Content as Content;
use protoware\cms\Count as Count;
include __DIR__ . '/vendor/autoload.php';
Local this works fine, when i upload it on a server i get this
Europe/Brussels] PHP Fatal error: Uncaught Error: Class 'protoware\cms\Content' not found in /data/vhosts/achielvolckaert.be/wwwroot/api.php:97
Stack trace:
#0 {main}
thrown in /data/vhosts/achielvolckaert.be/wwwroot/api.php on line 97
line 97 is the first line where i open a connection:
$content = new protoware\cms\Content();
$data = $content->get_content('medisch');
I have 0 clue how to fix this, i updated autloader, checked if all files were uploaded...
Try add a \ before class name, like $content = new \protoware\cms\Content(); or just $content = new Content() since you have a use statement.
If this class is a dependency, check if its correctly installed (inside your vendor folder, i.e.). Maybe run composer install?

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');

Categories