I'm trying to make my PHAR archive marked as PHP (I don't know correct term for this) so that I can use it from cli without php part. So instead of calling php /path/to/my/archive.phar ARGUMENTS_HERE I can just move it to /usr/local/bin and call it archive from anywhere. (Just like PHPUnit or Composer).
If I'm right, to do that, I just need to add #!/usr/bin/env php on top of my PHAR archive file?
How do I do that? I tried to mimic composer compiler in my build script, but it just crashes PHP_CLI (segmentation fault):
$archiveName = 'phpbenchmark.phar';
$phar = new Phar($archiveName);
$phar->buildFromDirectory(__DIR__, '/\/lib\/|\/vendor\//');
$phar->setStub(createStub());
function createStub()
{
$stub = <<<EOD
#!/usr/bin/env php
<?php
Phar::mapPhar('phpbenchmark.phar');
require 'phar://phpbenchmark.phar/lib/app.php';
__HALT_COMPILER();
EOD;
return $stub;
}
As you can see, I only need stub to run /lib/app.php file and that's it.
Prepending to compiled PHAR
Of course, first thing I tried was manually prepending it to my archive, but it looks like that corrupts it:
PHP Fatal error: Uncaught exception 'PharException' with message 'manifest cannot be larger than 100 MB in phar "/home/igor/Dropbox/www/Github/PIFlexPHPBenchmark/phpbenchmark.phar"' in /home/igor/Dropbox/www/Github/PIFlexPHPBenchmark/phpbenchmark.phar:9
Stack trace:
#0 /home/igor/Dropbox/www/Github/PIFlexPHPBenchmark/phpbenchmark.phar(9): Phar::webPhar(NULL, 'index.php')
#1 {main}
thrown in /home/igor/Dropbox/www/Github/PIFlexPHPBenchmark/phpbenchmark.phar on line 9
Fatal error: Uncaught exception 'PharException' with message 'manifest cannot be larger than 100 MB in phar "/home/igor/Dropbox/www/Github/PIFlexPHPBenchmark/phpbenchmark.phar"' in /home/igor/Dropbox/www/Github/PIFlexPHPBenchmark/phpbenchmark.phar on line 9
PharException: manifest cannot be larger than 100 MB in phar "/home/igor/Dropbox/www/Github/PIFlexPHPBenchmark/phpbenchmark.phar" in /home/igor/Dropbox/www/Github/PIFlexPHPBenchmark/phpbenchmark.phar on line 9
Call Stack:
0.0010 306272 1. {main}() /home/igor/Dropbox/www/Github/PIFlexPHPBenchmark/phpbenchmark.phar:0
0.0011 307840 2. Phar::webPhar() /home/igor/Dropbox/www/Github/PIFlexPHPBenchmark/phpbenchmark.phar:9
Note that exactly the same archive works before I manually modify it. (But needs php /path/to/arch of course)
Aaaand, solved (kinda) after few hours of breaking my head. I still have no idea why, this build script seems to work:
$archiveName = 'phpbenchmark.phar';
$phar = new Phar($archiveName);
$phar->buildFromDirectory(__DIR__, '/\/lib\/|\/vendor\//');
$phar->setStub(createStub());
function createStub()
{
$stub = <<<ENDSTUB
#!/usr/bin/env php
<?php
Phar::mapPhar('phpbenchmark.phar');
require 'phar://phpbenchmark.phar/lib/app.php';
__HALT_COMPILER();
ENDSTUB;
return $stub;
}
Except.....I can't really see any differences to original script posted in my question? Why would original one crash PHP Cli and this one works?
All I needed to do to get that to work is update the line:
$phar->setStub($phar->createDefaultStub("main.php"));
to :
$phar->setStub("#!/usr/bin/env php\n\n" . $phar->createDefaultStub("main.php"));
This takes the default stub that phar creates for you, and adds the requires #! line to it. Then just chmod 700 the resulting phar file, and you are good to go.
Related
If i have some PDF files, each has one page and i would like to merge all files into on single file using PHP
I have read dozens of questions here about same but most of all are out of date or no longer working since for PHP version >= 7.0
One of the solutions was to use PDFMerger and here is my code
require_once ('PDFMerger.php');
use PDFMerger\PDFMerger;
$pdf = new PDFMerger;
$pdf->addPDF('books/1.pdf');
$pdf->addPDF('books/2.pdf');
$pdf->merge('download','books/merged.pdf');
but if simply gives blank page and no file is created and same to this library PDFMerger
UPDATE1! wihtout namespace use PDFMerger\PDFMerger; iam getting this error
Fatal error: Uncaught Error: Class "PDFMerger" not found in mypath\sample.php:4 Stack trace: #0 {main} thrown in mypath\sample.php on line 4
so is there any way that is still working to merger PDF files without have to care how such file are created.
NOTE! I can not use shell_exec since files are on shared hosting
After trying a lot of time , i decide to shift back to a lower PHP version 7.1 rather than 8.0 and everything goes fine though
I am using this for ocr. This is wrapper for Tesseract ocr(I previosly installed Tesseract itself).
At first i dowloaded it via composer and followed examples in repo and also several posts on SO itself. And all can show is in network tab failed to load response data.
My alternate approaches is that i tried downloading repo itself, then tried to call it from my index.php which for test purposes is situated in same folder where class TesseractOCR is. I tried with images in repo and also tried with black letters on white background images with simple text.
This SO post looks promising, but i'm unsure where OP's file with example code is residing...
use thiagoalessio\TesseractOCR\TesseractOCR;
//or//require "TesseractOCR.php";//if it's in the same dir as test.php
$content = new TesseractOCR('text.png');
$text = $content->run();
echo $text;
Did i miss something obvious? Any help is appreciated.
EDIT1: I tried using in win powershell cli. By putting text.png in directory where tesseract is installed, then calling shell with administrator privileges, subsequently typing in it tesseract text.png output which creates output.txt in same directory with recognized text from that image.
So tesseract its working, my implementation with php wrapper is not.
EDIT2:
Forgot to add, page itself shows:
This page isn’t working
localhost is currently unable to handle this request.
HTTP ERROR 500
Not sure why it happens.
Edit3:
My code:
try{
//use thiagoalessio\TesseractOCR\TesseractOCR;
require "./vendor/thiagoalessio/tesseract_ocr/src/TesseractOCR.php";
echo $temp;//It's value is set in TesseractOCR.php
$content = new TesseractOCR('text1.png');
$text = $content->run();
echo $text;
}
catch(Exception $e) {
echo 'Message: ' .$e->getMessage();
}
Value set in $temp variable is visible through state file path, so why TesseractOCRclass itself isn't?
Edit4:
Even if i put absolute path to TesseractOCR.php which holds class, in include statement, it doesn't work.
It throws this error:
Fatal error: Uncaught Error: Class 'TesseractOCR' not found in C:\xampp\htdocs\myocr\index.php:10 Stack trace: #0 {main} thrown in C:\xampp\htdocs\myocr\index.php on line 10
This is TesseractOCR.//echoed text from file that holds TesseractOCR class.
Inclusion path:
include ("C:/xampp/htdocs/myocr/vendor/thiagoalessio/tesseract_ocr/src/TesseractOCR.php");
If i use(which is suggested in repo readme, use thiagoalessio\TesseractOCR\TesseractOCR;, then it throws:
Fatal error: Uncaught Error: Class 'thiagoalessio\TesseractOCR\TesseractOCR' not found in C:\xampp\htdocs\myocr\index.php:10 Stack trace: #0 {main} thrown in C:\xampp\htdocs\myocr\index.php on line 10
My question is: How it hits test message, but won't hit TesseractOCR class?
EDIT5:
If i require_once "./vendor/autoload.php"; , it throws:
Fatal error: Uncaught thiagoalessio\TesseractOCR\TesseractNotFoundException: Error! The command "tesseract" was not found. Make sure you have Tesseract OCR installed on your system: https://github.com/tesseract-ocr/tesseract The current $PATH is C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\Git\cmd;C:\Program Files\nodejs\;C:\xampp\php;C:\ProgramData\ComposerSetup\bin;C:\Users\Eddie\AppData\Local\Microsoft\WindowsApps;;C:\Users\Eddie\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\Eddie\AppData\Roaming\npm;C:\Users\Eddie\AppData\Roaming\Composer\vendor\bin;C:\Program Files\heroku\bin in C:\xampp\htdocs\myocr\vendor\thiagoalessio\tesseract_ocr\src\FriendlyErrors.php:48 Stack trace: #0 C:\xampp\htdocs\myocr\vendor\thiagoalessio\tesseract_ocr\src\TesseractOCR.php(26): thiagoalessio\TesseractOCR\FriendlyErrors::checkTesseractPresence('tesseract') #1 C:\xampp\ht in C:\xampp\htdocs\myocr\vendor\thiagoalessio\tesseract_ocr\src\FriendlyErrors.php on line 48
Btw, i added its patch to env variable:
I solved it!. My problem occurs that i didn't know about autoloaders in php.
Link that helped me is this.
My project structure is this:
Created project folder myocr.
After previous, downloaded latest stable version of Tesseract and installed it.
Depending on your system, you may be required to add value to your system env variable. You need to do that here
then
then
then
I'm assuming it self explanatory with images provided.
Next is getting TesseractOCR via composer:
composer require thiagoalessio/tesseract_ocr
Finally, before using code sample in repo, you need to call autoloader.
Index.php:
require_once('./vendor/autoload.php');//<-This!
use thiagoalessio\TesseractOCR\TesseractOCR;
$content = new TesseractOCR('text1.png');
$text = $content->run();
echo $text;
This worked for me. Crucial thing to look after is directory structure.
localhost > myocr > index.php with its code, and after using composer you'll get vendor dir. and it's content. Image path in new TesseractOCR('text1.png'); is directory where both index.php and image are located.
I want To run a symfony project in my computer but I get all times errors , and now I get this error and i didn't find any solution for my problem
Problem Failed to parse output as xml: Error on line 2: Content is not
allowed in prolog.. Command C:\xampp\php\php.exe
C:\xampp\htdocs\buzzaka\bin\console list --format=xml Output
Fatal error: Uncaught
Symfony\Component\Dotenv\Exception\PathException: Unable to read the
"C:\xampp\htdocs\buzzaka\bin/../.dev.env" environment file. in
C:\xampp\htdocs\buzzaka\vendor\symfony\dotenv\Dotenv.php:466 Stack
trace:
0 C:\xampp\htdocs\buzzaka\vendor\symfony\dotenv\Dotenv.php(51): Symfony\Component\Dotenv\Dotenv->doLoad(false, Array)
1 C:\xampp\htdocs\buzzaka\bin\console(38): Symfony\Component\Dotenv\Dotenv->load('C:\xampp\htdocs...')
2 {main} thrown in C:\xampp\htdocs\buzzaka\vendor\symfony\dotenv\Dotenv.php on line 466
If u move your file from the folder where it was created symfony will keep looking for it on this folder, i've went on autoload_runtime and changed on $runtime variable where the project is, just worked for me, lets see on the last composer dump what just happens haha
The error is quite explicit, do you have a file C:\xampp\htdocs\buzzaka\bin/../.dev.env ?
Well, you need to create one. As announced a little while ago, Symfony will now require a .env file per environment. As you are running your command in the dev environment, you need to create a .dev.env file.
I am attempting to use https://github.com/amzn/amazon-instant-access-sdk-php for amazon instant access via php. I am not super familiar with phar files; but am starting to figure it out.
I included https://github.com/amzn/amazon-instant-access-sdk-php/blob/master/phar-stub.php in a file as shown below. (Modified a bit as I renamed phar file)
Phar::mapPhar('amazon-instant-access-sdk-php.phar');
require_once 'phar://amazon-instant-access-sdk-php.phar/vendor/symfony/class-loader/Symfony/Component/ClassLoader/UniversalClassLoader.php';
$classLoader = new Symfony\Component\ClassLoader\UniversalClassLoader();
$classLoader->registerNamespaces(array(
'Amazon' => 'phar://amazon-instant-access-sdk-php.phar/src',
'Psr' => 'phar://amazon-instant-access-sdk-php.phar/vendor/psr/log',
'Monolog' => 'phar://amazon-instant-access-sdk-php.phar/vendor/monolog/monolog/src'
));
$classLoader->register();
return $classLoader;
__HALT_COMPILER();
Then when I do in another file:
require_once('amazon-instant-access-phar-stub.php');
I get error:
Fatal error: Uncaught exception 'PharException' with message 'internal corruption of phar "/Applications/MAMP/htdocs/phppos/amazon-instant-access-phar-stub.php" (truncated manifest at stub end)' in /Applications/MAMP/htdocs/phppos/amazon-instant-access-phar-stub.php:17 Stack trace: #0 /Applications/MAMP/htdocs/phppos/amazon-instant-access-phar-stub.php(17): Phar::mapPhar('amazon-instant-...') #1 /Applications/MAMP/htdocs/phppos/amazon_link_account.php(3): require_once('/Applications/M...') #2 {main} thrown in /Applications/MAMP/htdocs/phppos/amazon-instant-access-phar-stub.php on line 17
I have tried re-downloading phar file and running different versions of php (5.3, 5.6)
I am not sure what is causing this error.
You could directly download the .phar file from the release.
If you have the .phar file, you don't need the source code from github. The source code on github is used to compile the .phar file.
In your php code, you could do something like this:
<?php
//import the phar file directly like you did
require_once('amazon-instant-access-sdk-php.phar’);
//before you initiate the class, you need to use the right name space:
use Amazon\InstantAccess\Signature as signature;
$credentialStore = new signature\CredentialStore();
?>
This one will work.
If you want to check exactly what's in the .phar file, you could extract it:
php -r '$phar = new Phar("amazon-instant-access-sdk-php.phar"); $phar->extractTo("/tmp/phar/");'
then you could browse the /tmp/phar/ for all the files in the .phar.
I have some issue with my production deployment of Symfony2,
I've tried many solutions, but none have worked.
I randomly have this error when accessing my symfony application on production environment:
( ! ) Fatal error: Uncaught exception 'Symfony\Component\Debug\Exception\ContextErrorException' with message 'Warning: simplexml_load_file(): I/O warning : failed to load external entity "/home/user/symfony/vendor/friendsofsymfony/user-bundle/Resources/config/doctrine/model/User.orm.xml"' in /home/user/symfony/app/bootstrap.php.cache on line 2998
( ! ) Symfony\Component\Debug\Exception\ContextErrorException: Warning: simplexml_load_file(): I/O warning : failed to load external entity "/home/user/symfony/vendor/friendsofsymfony/user-bundle/Resources/config/doctrine/model/User.orm.xml" in /home/user/symfony/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php on line 736
Call Stack
# Time Memory Function Location
1 0.0000 262880 {main}( ) ../app_dev.php:0
2 0.0015 572736 Symfony\Component\HttpKernel\Kernel->handle( ) ../app_dev.php:79
3 0.1342 4023952 Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel->handle( ) ../bootstrap.php.cache:2376
( ! ) LogicException: Request stack is empty in /home/user/symfony/app/bootstrap.php.cache on line 2998
Call Stack
# Time Memory Function Location
1 0.3330 7110120 Symfony\Component\Debug\ErrorHandler->handleException( ) ../classes.php:0
2 0.3331 7119696 Symfony\Component\Debug\ErrorHandler->handleException( ) ../classes.php:1939
I've tried to upgrade my php version (I was in php 5.4.x and now in 5.6.4),
I've tried to upgrade lixml2 version (i am in 2.8.0 now, but i already tried to upgrade in 2.9.3)
I've constated that the version of libxml used in php is always 2.8.0, but, I haven't found the way to change this ,
I've tried to set the all directory of symfony in chmod 777
My server is a debian 7.5 server.
Maybe someone who knows this error can help me
Here is some links to differents question related to this one:
Random Error, FOSUserBundle Error and Service error
I didn't post in them because they're all outdated
[EDIT]
I found a quick fix, but it's in vendors, so it will be overrided in the first update of the doctrine update:
QuickFix in XmlDriver.php Line 737
$xmlElement = #simplexml_load_file($file);
if(!$xmlElement){
$xmlData = file_get_contents($file);
$xmlElement = simplexml_load_string($xmlData);
}
We got this error after we started using libxml_disable_entity_loader(true); in our code. That code is vital in preventing XXE -attacks(more info of that in here). If you don't have that in your code, it could be that you have installed/updated a bundle which has that line of code in use. Note that libxml_disable_entity_loader() is not thread safe, so if there is one piece of code on one thread that executes that line, everything on that server now has that enabled throughout the process.
The FOS-bundle seems to use xml-definitions, that in return have external entities in them. Nothing major though, but that code prevents the FOS-bundles methods from using those files correctly.
Luckily, our service got that error only in one place, and the fix was obvious:
Add a libxml_disable_entity_loader(false); before executing that piece of code where the error comes from, and add libxml_disable_entity_loader(true); right after that piece of code.
This way the user bundle could load the xml:s it needed, but security is not compromised.
Example:
libxml_disable_entity_loader(false);
$user = $query->getOneOrNullResult(); // This generates an error if entity loader is disabled
libxml_disable_entity_loader(true);
The error mentioned usually happens when the xml file does not exists.
You can try check if the file really exists after try to load:
if (file_exists($file)) {
$xmlElement = #simplexml_load_file($file);
}
I tried to simulate the error locally running php interactively, the results are in down:
php > simplexml_load_file('test.xml');
PHP Warning: simplexml_load_file(): I/O warning : failed to load external entity "test.xml" in php shell code on line 1
php >
Note, the error above is the same that you mentioned on the question. So, the first try is use the file_exists() function to check if the file really exists after try to load it. The next step is check if the current path - using getcwd() - and the path of the file are pointing correctly to the file.
I created a file called test.xml on /tmp folder:
<test>
<worked>
</worked>
</test>
Called the interactive mode on php:
$ php -a
Interactive mode enabled
So, checked my local directory:
php > echo getcwd();
/tmp
And, tried to load the file:
php > $data = simplexml_load_file('test.xml');
php > var_dump($data);
object(SimpleXMLElement)#1 (1) {
["worked"]=>
object(SimpleXMLElement)#2 (1) {
[0]=>
string(2) "
"
}
}
Note: that worked correctly. So, probably, your file does not exists or the path are incorrect. The path considers the current path /tmp plus, the file indicated on the function call test.txt, or equals to call the /tmp/test.txt file.
In my case, the problem on new hosting was:
allow_url_fopen
was disabled (any simplexml_load_file() or file_get_contents() returned false).
Solution: enable allow_url_fopen.
There are some solutions given:
https://github.com/FriendsOfSymfony/FOSUserBundle/issues/1062
https://github.com/symfony/symfony/issues/7291