phives Posted September 13, 2023 Share Posted September 13, 2023 I am using open source PDF and Excel software. All run perfectly in previous versions of PHP. However, when moving code to 8.1 the following error is reported. In this case it happens to be with PhpSpreadsheet. But it also happens with other packages. Appears to find the file correctly but then has issues. I've been using PHP for a while, but I'm not really familiar with the inner workings. Uncaught Error: Class "var\\www\\html\\STG\\PhpOffice\\PhpSpreadsheet\\Spreadsheet" not found Here is the code I am using: use \var\www\html\STG\PhpOffice\PhpSpreadsheet\Spreadsheet; use \var\www\html\STG\PhpOffice\PhpSpreadsheet\Writer\Xlsx; $spreadsheet = new Spreadsheet(); $activeWorksheet = $spreadsheet->getActiveSheet(); $activeWorksheet->setCellValue('A1', 'Hello World !'); $writer = new Xlsx($spreadsheet); $writer->save('hello world.xlsx'); Quote Link to comment Share on other sites More sharing options...
kicken Posted September 13, 2023 Share Posted September 13, 2023 You don't specify a filesystem path when you use a class, you specify it's namespace path. use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Writer\Xlsx; As shown in the documentation example. You also need to include the autoload.php file (again, see the example) or the class will not get loaded. Quote Link to comment Share on other sites More sharing options...
phives Posted September 13, 2023 Author Share Posted September 13, 2023 (edited) Where do I find the autoload file? Here is error message ubuntu@ip-172-31-91-195:~$ composer require phpoffice/phpspreadsheet Installation failed, deleting ./composer.json. In RequireCommand.php line 210: No composer.json present in the current directory (./composer.json), this may be the cause of the following exception. In InitCommand.php line 904: Package phpoffice/phpspreadsheet has requirements incompatible with your PHP version, PHP extensions and Composer version: - phpoffice/phpspreadsheet 1.29.0 requires ext-dom * but it is not present. - phpoffice/phpspreadsheet 1.29.0 requires ext-simplexml * but it is not present. - phpoffice/phpspreadsheet 1.29.0 requires ext-xml * but it is not present. - phpoffice/phpspreadsheet 1.29.0 requires ext-xmlreader * but it is not present. - phpoffice/phpspreadsheet 1.29.0 requires ext-xmlwriter * but it is not present. - phpoffice/phpspreadsheet 1.29.0 requires ext-zip * but it is not present. require [--dev] [--dry-run] [--prefer-source] [--prefer-dist] [--prefer-install PREFER-INSTALL] [--fixed] [--no-suggest] [--no-progress] [--no-update] [--no-install] [--update-no-dev] [-w|--update-with-dependencies] [-W|--update-with-all-dependencies] [--with-dependencies] [--with-all-dependencies] [--ignore-platform-req IGNORE-PLATFORM-REQ] [--ignore-platform-reqs] [--prefer-stable] [--prefer-lowest] [--sort-packages] [-o|--optimize-autoloader] [-a|--classmap-authoritative] [--apcu-autoloader] [--apcu-autoloader-prefix APCU-AUTOLOADER-PREFIX] [--] [<packages>...] Edited September 13, 2023 by phives Quote Link to comment Share on other sites More sharing options...
kicken Posted September 13, 2023 Share Posted September 13, 2023 51 minutes ago, phives said: Where do I find the autoload file? It is generated by composer once all your dependencies have been installed. It appears you may not be familiar with composer, so you should probably take some time to read it's documentation and get familiar with it and how it works. 53 minutes ago, phives said: Package phpoffice/phpspreadsheet has requirements incompatible with your PHP version, PHP extensions and Composer version: - phpoffice/phpspreadsheet 1.29.0 requires ext-dom * but it is not present. - phpoffice/phpspreadsheet 1.29.0 requires ext-simplexml * but it is not present. - phpoffice/phpspreadsheet 1.29.0 requires ext-xml * but it is not present. - phpoffice/phpspreadsheet 1.29.0 requires ext-xmlreader * but it is not present. - phpoffice/phpspreadsheet 1.29.0 requires ext-xmlwriter * but it is not present. - phpoffice/phpspreadsheet 1.29.0 requires ext-zip * but it is not present. These errors mean that your PHP installation is missing extensions that are required by the phpoffice/phpspreadsheet package. Specifically, you must install the DOM Extension, SimpleXML extension, XML extension, XML Reader extension, XML Writer extension, and ZIP extension. If you obtained PHP through your systems package manager, it will probably also have packages for these extensions that you need to install. You'll have to do some research on your particular package manager to find out what these packages are and get them installed. Quote Link to comment Share on other sites More sharing options...
phives Posted September 13, 2023 Author Share Posted September 13, 2023 I installed all the extensions. I then ran the composer. Per composer instructions it was supposed to create a vendor.autoload.php. Alas it did not. Quote Link to comment Share on other sites More sharing options...
maxxd Posted September 13, 2023 Share Posted September 13, 2023 (edited) What errors did you get? Assuming you ran composer install phpoffice/phpspreadsheet as per the instructions, you'll have a composer.json and composer.lock file in your main directory, and an autoload.php in the /vendor directory. Edited September 13, 2023 by maxxd hit enter too early Quote Link to comment Share on other sites More sharing options...
phives Posted September 13, 2023 Author Share Posted September 13, 2023 Do I have to first copy all the PhpSpreadsheet files onto the server and then run composer? Quote Link to comment Share on other sites More sharing options...
maxxd Posted September 13, 2023 Share Posted September 13, 2023 No, composer is a package manager - it'll reach out and download everything you need. Of course, in order to actually serve your site you'll need to get the files to a server, but that happens after you run composer install from the command line. Quote Link to comment Share on other sites More sharing options...
phives Posted September 13, 2023 Author Share Posted September 13, 2023 I admit I am lost. I run composer then what? This is what my folder structure looks like on the server. Will composer create the new folders or do I have to do that. Can you give me a quick order of actions please. Quote Link to comment Share on other sites More sharing options...
maxxd Posted September 14, 2023 Share Posted September 14, 2023 OK - couple questions. Can you get to the site main directory (also known as the document root) on the command line, and are you working locally or on a remote server? If the answer to the first question is yes then the answer to the second matters less, but if so please come back and we'll all talk about development best practices. So let's assume your working directory is your document root and is located at /var/www/html. First and foremost, you'll use your terminal application to navigate into that directory: cd /var/www/html Then, you use Composer to install the package: composer require phpoffice/phpspreadsheet You should now see a /var/www/html/vendor directory. Again, in this example your php files should be located in /var/www/html. Then, in your php files, you'll add the following in order to use the package: <?php require_once($_SERVER['DOCUMENT_ROOT'].'/vendor/autoload.php'); use PhpOffice\PhpSpreadsheet\Spreadsheet; $spreadsheet = new Spreadsheet(); Just to be perfectly clear, this is not the best structure for a website - you should have all your vendor files above the document root and they should be included by the files in your document root, but that's a step too far for right now and I don't want to overwhelm. Quote Link to comment Share on other sites More sharing options...
kicken Posted September 14, 2023 Share Posted September 14, 2023 Assuming you start with a new, empty directory, running composer require phpoffice/phpspreadsheet would give you output like this: keith@keith-framework:~/Workspace/Demo$ composer require phpoffice/phpspreadsheet ./composer.json has been created Running composer update phpoffice/phpspreadsheet Loading composer repositories with package information Updating dependencies Lock file operations: 9 installs, 0 updates, 0 removals - Locking ezyang/htmlpurifier (v4.16.0) - Locking maennchen/zipstream-php (3.1.0) - Locking markbaker/complex (3.0.2) - Locking markbaker/matrix (3.0.1) - Locking phpoffice/phpspreadsheet (1.29.0) - Locking psr/http-client (1.0.2) - Locking psr/http-factory (1.0.2) - Locking psr/http-message (2.0) - Locking psr/simple-cache (3.0.0) Writing lock file Installing dependencies from lock file (including require-dev) Package operations: 9 installs, 0 updates, 0 removals - Downloading psr/simple-cache (3.0.0) - Downloading psr/http-message (2.0) - Downloading markbaker/matrix (3.0.1) - Downloading markbaker/complex (3.0.2) - Downloading maennchen/zipstream-php (3.1.0) - Downloading ezyang/htmlpurifier (v4.16.0) - Downloading phpoffice/phpspreadsheet (1.29.0) - Installing psr/simple-cache (3.0.0): Extracting archive - Installing psr/http-message (2.0): Extracting archive - Installing psr/http-factory (1.0.2): Extracting archive - Installing psr/http-client (1.0.2): Extracting archive - Installing markbaker/matrix (3.0.1): Extracting archive - Installing markbaker/complex (3.0.2): Extracting archive - Installing maennchen/zipstream-php (3.1.0): Extracting archive - Installing ezyang/htmlpurifier (v4.16.0): Extracting archive - Installing phpoffice/phpspreadsheet (1.29.0): Extracting archive 9 package suggestions were added by new dependencies, use `composer suggest` to see details. Generating autoload files 1 package you are using is looking for funding. Use the `composer fund` command to find out more! No security vulnerability advisories found Using version ^1.29 for phpoffice/phpspreadsheet And you would end up with a directory structure like: Running in your existing /var/www/html/STG folder, you should see the same output, with the same folders mixed in to your existing stuff. If you're getting some other output, then posting the output would help determine the issue. Quote Link to comment Share on other sites More sharing options...
maxxd Posted September 14, 2023 Share Posted September 14, 2023 👆 - that. Quote Link to comment Share on other sites More sharing options...
Strider64 Posted September 14, 2023 Share Posted September 14, 2023 Here's a good website that helped me with composer when I first started out using it. https://devdojo.com/bobbyiliev/use-composer-like-a-pro-the-dependency-manager-for-php It might not help you with this project, but it'll give you a better understanding on how to use it. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.