YII2 moonlandsoft\phpexcel\Excel::import file does not exist error - php

I have a requirement to fetch data from an excel file through a widget (i.e) moonlandsoft/phpexcel/Excel in YII2. For testing, I am trying to place the file in the appropriate view directory (Where I have the below coding in the view file) and test the code. But an error saying
file doesn't exist
is occurring. Its because I don't know the exact location where the widget trying to fetch data. Where should I place the excel file ( test.ods ) to test?
<?php
use moonland\phpexcel\Excel;
$filename = "test.ods";
$data = Excel::import(
$filename, [
'setFirstRecordAsKeys' => true
]
);
print_r($data);
exit;

You need to place the file inside the web directory and not in the views directory as web folder is the place which is publically accessible and includes the entry script index.php.
So place your file in the web directory if you are using
yii2-app-basic YOUR_PROJECT_ROOT/web
yii2-advance-app YOUR_PROJECT_ROOT/frontend/web or YOUR_PROJECT_ROOT/backend/web
and your script will start working and you can see the array printed.
Apart from getting it working, my opinion is you should not use this extension because
It is using the PHPExcel which is deprecated and replaced by PHPSpreadsheet you can see the message on the console when you install this extension for the first time
Package phpoffice/phpexcel is abandoned, you should avoid using it.
Use phpoffice/phpspreadsheet instead.
PhpSpreadsheet is the next version of PHPExcel. It breaks compatibility to dramatically improve the code base quality (namespaces, PSR compliance, use of latest PHP language features, etc.).
Because all efforts have shifted to PhpSpreadsheet, PHPExcel will no longer be maintained. All contributions for PHPExcel, patches and new features, should target PhpSpreadsheet develop branch.
And above all the extension is abandoned and no more maintained since 2016 there are several pull request that was never merged including the update to PHPSpreadsheet but nothing.
A better alternative could be arieslee/yii2-phpspreadsheet which is a modified version of the same extension you are using and upgraded to PHPSpreadsheet, you won't need to change anything in the code its the same extension with same methods.

Related

How do you add a vendor javascript asset to a large "vendor.js" with Laravel Mix, if the asset is not available with npm?

For example, both jquery and jstree are available with npm.
Therefore, in my Laravel project I can add this to the file "webpack.mix.js":
mix.js('resources/js/app.js', 'public/js').extract(['jquery','jstree']);
When running "npm run dev" then indeed both jquery and jstree ends up in the file "public/js/vendor.js" as expected according to the documentation about Vendor Extraction (which uses "extract(['vue'])" in the example)
As far as I understand, this works because both jquery and jstree are located in "node_modules" thanks to being defined in my "package.json" (by defining "jquery": "^3.2" and "jstree": "^3.3.9")
However, how should I use Laravel Mix to put some other javascript file asset into my large generated "vendor.js" file if that other asset is not available with npm but for example I instead retrieved it with composer?
For example the file "jsvalidation.js" from laravel-jsvalidation
I added that dependency into my project with "composer.json" (by using "proengsoft/laravel-jsvalidation": "^3.0") and then ran "composer update".
Then I used "mix.copy" in "webpack.mix.js" since I have found some info about the jsvalidation and laravel-mix:
Quote: "Keep in mind to copy your files using mix, so add in webpack.mix.js file":
mix.copy('vendor/proengsoft/laravel-jsvalidation/public/js/jsvalidation.min.js', 'public/vendor/jsvalidation/js/');
(as you might notice the above linked page is for "Laravel-6-installation" but currently there seems to be no documentation for Laravel 7 which I am using, but anyway, regarding the laravel-mix stuff I am asking about it should not be depending on which version of the jsvalidation library I am using)
So I have added the above "mix.copy" row to my "webpack.mix.js" file but then what should I to get that jsvalidation file into the large "vendor.js" together with jquery and jstree?

How to use PhpSpreadsheet without installation (like PHPExcel)

According to the PhpSpreadsheet Doc it's neccessary to install it with composer.
In my case I just have a webspace without Terminal but Plesk. Is it anyway possible to use PhpSpreadsheet, like it is with PHPExcel where you just have to place the files in any location?
What do I have to do to get it run? I found no further information how to with only FTP webserver access.
In your case there are two options for you!
Answer: 1
Alternative method without terminal
Run composer with a PHP script in browser
Answer: 2
Third party sites, which allow to download composer packages online. get PHPspreadsheet latest version.
https://php-download.com/package/phpoffice/phpspreadsheet
Bonus You can download almost any composer packages # https://php-download.com
Have you considered installing composer locally, running it as instructed, and then just sending the generated files to the server via normal FTP? You install composer on your computer and run it there, then upload via FTP/SFP...
Composer is not "necessarily" intended to run live while the user is downloading the pages, it is intended to be run "mainly" on your local computer and generating the files and dependencies that later on you upload to the server. It can update the files on your server, but that is a convenience not a necessity.
By registering custom autoloader and PSR simplecache autoloader it is possible to fully workaround composer installation - see:
https://github.com/PHPOffice/PhpSpreadsheet/issues/31#issuecomment-354502740
Please note, that installation via composer is currently the only officially supported solution.
I had the same problem. I downloaded Library on github from
https://github.com/PHPOffice/PhpSpreadsheet
and made a few changes
changed its namespaces
used autoload.php file
It worked
the Library without the need of Composer is in this link
https://github.com/YoloZoloo/PhpSpreadSheet/tree/master
You can change the folder name to any folder you like.
Load [AnyFolder]->table.php from your server and press 「ダウンロード」.
Hit back to me if you encounter any issues
sample code
require 'vendor/autoload.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');
Smartpal, thanks for your pack, it works fine!
Also it possible to replace folder:
/phpspreadsheet/vendor/phpoffice/phpspreadsheet/src/
in Smartpal's archive with more fresh sources from phpspreadsheet github and it also will work.
PS: If you have issues with reading xls files saved in encoding other than CP1252, you have to seek literals CP1252 in file ./src/PhpSpreadsheet/Reader/Xls.php and replace them with other value that you need or some named constant or even mb_detect_encoding(). Then it will read such xls correctly.

I want to fix one issue during file moving in Laravel 5.3

I am developing invoice management system with Laravel framework (version 5.3). The system provides a list of documents in a directory on the server. The documents are JPEG or PDF files, they are saved in DB with some metadata. After save a document data in DB, I want to move it to other directory on server.
I tried it by using File::move($source_path, $target_path) and Storage::move($source_path, $target_path). But in two case, errors are occurred.
My code is
$save_pdf_path = storage_path('invoices').'\\'.$company.'\\'.$date.'\\';
$pdf_name = $invoice_id."_".$user_id.".pdf";
$full_pdf_path = $save_pdf_path.$pdf_name;
File::move(public_path().'\invoice_tmp\\'.$filename, $full_pdf_path);
or
Storage::move(public_path().'\invoice_tmp\\'.$filename, $full_pdf_path);
When use File:move(..)
When use Storage:move(..)
At last, I tried PHP core function rename(), but it is not working too.

what i need to consider when building phar file from php application use php framework like laravel

I am php developer use laravel-4 as framework for building web applications , in the last few days I wanted to create phar file from my web application created on laravel framework .
I searched in the web for tools build php archive files (.phar) and I found
PHP box , this tool is very good and use json configuration file for building the phar files but i could not use it for creating my phar files because there is many considerations when creating phar file from a web application use a framework like laravel . my questions are :
laravel use composer autoloader as auto loading mechanism
1- how to handle composer auto loading mechanism ?
2- how to handle the framework bootstrapping process ? 'like laravel'
3- what i need to make the browser read my index page from inside the phar file ?
4- how to use framework command line tools from the phar file ? 'like laravel-artisan'
You might use composer.json in your application and require box there.
When your application runs on laravel, you know that your bootstrap works and would also work inside a phar.
I believe Box brings a lot of the composer autloading stuff itself, so you won't run into trouble with it. I think the class_map gets included automatically.
One thing to consider is, that configuration details must be passed in!
In general, you need to "forward" to your application, which is inside the phar, like so:
<?php
require_once "phar://myapp.phar/frontcontroller.php"; // maybe index.php
$config = array('dsn' => 'database-config');
Application::run($config);
Also accessing a PHAR in a PHAR is a problem!
You can't access a PHAR packaged in a PHAR directly.
Firstly you need to extract the packaged PHAR, secondly do the forwarding call and pass the CLI commands along. Problem solved here: https://stackoverflow.com/a/13537329/1163786
Full Example
box.json.dist
{
"main": "bootstrap.php",
"output": "application.phar",
"compactors": ["Herrera\\Box\\Compactor\\Composer"],
"chmod": "0755",
"directories": ["src/"],
"stub": true
}
bootstrap.php
<?php
require 'vendor/autoload.php'; //<-- this is autoload.php generated by Composer
use MyApp\Application;
$config = parse_ini_file(__DIR__.'/config.ini');
$app = new Application();
$app->run($config);

aws-php-sdk -- Installing without a package manager

I'm working on pulling in the AWS PHP SDK and am running into some issues since my current stack isn't using a package manager. It's not an option to start using it, either (company related -- would rather not elaborate).
That all being said, I'm pulling in the source directly and trying to add it to my include path and just including the files as I need them in my S3 wrapper objects that I'm writing. However, it's running into issues with the namespaces (I think) and those blowing up.
This is the library I'm referring to:
https://github.com/aws/aws-sdk-php
I tried following the bit at the bottom about working with AmazonS3 and uploading a file to it. So, I attempted to include the various parts of the code it referenced as follows:
Attempt One
require_once('/includes/third_party/aws-sdk-php-master/src/Aws/Common/Aws.php');
require_once('/includes/third_party/aws-sdk-php-master/src/Aws/S3/Enum/CannedAcl.php');
require_once('/includes/third_party/aws-sdk-php-master/src/Aws/S3/Exception/S3Exception.php');
Attempt Two
set_include_path(get_include_path() . "/includes/third_party/aws-sdk-php-master/src/");
include('Aws/Common/Aws.php');
include('Aws/S3/Enum/CannedAcl')
include('Aws/S3/Exception/S3Exception.php');
Both of these produced a similar error:
Fatal error: Class 'Guzzle\Service\Builder\ServiceBuilderLoader' not found in \includes\third_party\aws-sdk-php-master\src\Aws\Common\Aws.php on line 26
PHP Fatal error: Class 'Guzzle\Service\Builder\ServiceBuilderLoader' not found in \includes\third_party\aws-sdk-php-master\src\Aws\Common\Aws.php on line 26
Any advice on how to start debugging this? Would be much appreciated!
The AWS SDK for PHP nows ships a zip file with everything you need, including an autoloader: http://docs.aws.amazon.com/aws-sdk-php-2/guide/latest/installation.html#installing-via-zip.
You should stick the the recommend installation procedures. Personally, I would go with the Composer installation or just use the PHAR without Composer.
Then you just need to include the PHAR like:
require '/path/to/aws.phar';
And you will have everything you need.
The problem you have now is likely that you are not taking advantage of the autoloader. Using your approach, you will need to manually include all the classes that would normally be autoloaded.
You also need to download and include another library (Guzzle, not included in the PHAR archive).

Categories