im a PHP newbie . .
i've tried to make a PHAR of my project.
my project include 5 directories and multiply classes within each dir.
I've followed this tutorial
Phar – PHP archiving practice
and i did the following:
to my main index.php i added this lines:
$sLibraryPath = 'lib/myPhar.phar';
// we will build library in case if it not exist
if (! file_exists($sLibraryPath)) {
ini_set("phar.readonly", 0); // Could be done in php.ini
$oPhar = new Phar($sLibraryPath); // creating new Phar
$oPhar->setDefaultStub('index.php', 'classes/index.php'); // pointing main file which require all classes
$oPhar->buildFromDirectory('classes/'); // creating our library using whole directory
$oPhar->compress(Phar::GZ); // plus - compressing it into gzip
}
and i created a new folder called classes , and created a index.php file within it, and there i included all my project classes.
i've chmod 777 the following directories: lib, HelloWorld , HelloWorld/index.php ,
and changed the phar.readonly. (phar.readonly = 0)
still i get this message:
Fatal error: Uncaught exception 'PharException' with message 'unable to create temporary file' in /Applications/XAMPP/xamppfiles/htdocs/HelloWorld/index.php:10 Stack trace: #0 /Applications/XAMPP/xamppfiles/htdocs/HelloWorld/index.php(10): Phar->setDefaultStub('index.php', 'classes/index.p...') #1 {main} thrown in /Applications/XAMPP/xamppfiles/htdocs/HelloWorld/index.php on line 10
i havent found any helpful info what to do with it, and i will glad for your help.
(if there is a better way to create PHAR it will be fine)
I'm not sure if it is the point, but check this link Bug #63112 PharException when compiling
May be you have the same trouble with temp directory?
Use the PHP2Phar package to create your Phar file from PHP files.
Link:
https://github.com/00F100/php2phar
Usage:
$ php php2phar.phar --dir-source <path/to/dir> --index-file </path/to/index.php> --output-file <path/to/file.phar>
Related
I'm writing a very basic catenation script and lifted this straight off the pdftk 0.10.0 packagist site:
use mikehaertl\pdftk\Pdf;
// Extract pages 1-2 into a new file
$pdf = new Pdf('sourcefile.pdf');
$result = $pdf->cat(1, 2)
->saveAs('newfile.pdf');
if ($result === false) {
$error = $pdf->getError();
}
But I'm getting this error:
Fatal error: Uncaught Error: Class "mikehaertl\pdftk\Pdf" not found in C:\PHP8\index.php:7 Stack trace: #0 {main} thrown in C:\PHP8\index.php on line 7
All the sample scripts I've seen have that same header. I'm sure it's something really basic that I'm not seeing here.
Since the error is showing the Class not found, it would make me initially think that the package wasn’t installed in your project. If you run composer require mikehaertl/php-pdftk, does that get you rolling?
If you’re not familiar with Composer, here’s a quick guide to getting started: https://packagist.org/
Apparently, when installed via composer, the pdftk.exe file is not downloaded for some reason. I downloaded it manually from the github page and referred to it in the header.
Ok, finally found the answer. You need to install pdftk beforehand as packagist does not automatically pull it for you.
I am new to php and have just installed my first package via composer. I'm now trying to call a function from the package I installed as follows:
<?php
require_once 'vendor/autoload.php';
$value = 1;
$aws = AmazonGiftCode::make()->buyGiftCard($value);
echo $aws;
?>
But I get the following error:
PHP Fatal error: Uncaught Error: Class 'AmazonGiftCode' not found in
/public_html/php/test.php:4 Stack trace:
#0 {main} thrown in /public_html/php/test.php on line 4
Based on my (albeit limited) experience with other languages, I'm guessing I have to load the package that contains the class first. The package folder is in the same directory as the test.php file, in the subfolder vendor/kamerk22/AmazonGiftCode/. But I think this is where I don't know enough to troubleshoot it based on the information I could find.
Your directory structure should look like this.
test.php
composer.json
vendor
└───autoload.php
└───kamerk22
└───AmazonGiftCode
Make sure you installed the package using composer, and not by downloading it.
I'm guessing I have to load the package that contains the class first.
Unlike what you would normally expect when importing stuff, when using composer you only have to import the autoload.php file, and composer will take care of loading other packages as needed. By as needed what I mean is that as soon as composer sees you use the AmazonGiftCode class it will import the AmazonGiftCode package, but if one of your REST endpoints doesn't use anything from the AmazonGiftCode package it won't ever load it. This allows you to not have to worry about slowing down the entire application when you want to use a composer package for only a few endpoints. At least that's the way I understand how composer works.
Just run composer dump-autoload once and the class should known then.
You could also get verbose and use kamerk22\AmazonGiftCode\AmazonGiftCode;
But that AmazonGiftCode looks quite Laravel specific... that's why it may still fail, even if it may be found by auto-load. One needs to setup Laravel framework in the first place; just see this query (just in case if you may wonder where all these missing classes may come from).
I've created a phar of a Symfony2 web application, but I'm having some troubles with the cache-folders.
I found out that I could mount an external file/folder into a phar. That would fix my issue, but I can't get the example on the PHP site working.
I have a phar which contains an index.php:
<?php
$configuration = simplexml_load_string(file_get_contents(
Phar::running(false) . '/config.xml'));
?>
Then I include the .phar with the following code:
<?php
// first set up the association between the abstract config.xml
// and the actual one on disk
Phar::mount('phar://config.xml', '/var/www/$projectname/config.xml');
// now run the application
include 'phar-archive.phar';
?>
All files exists, but I get the following error:
PHP Fatal error: Uncaught exception 'PharException' with message 'config.xml is not a phar archive, cannot mount' in /var/www/$projectname/index.php:3
I already tried relative/absolute paths, changing permissions but can't get this to work. Additionally a working example of how I could mount a folder into a phar would be great !
First check that variable $projectname exist in this place (just run echo $projectname; before Phar::mount). If all ok, then change
Phar::mount('phar://config.xml', '/var/www/$projectname/config.xml');
to
Phar::mount('phar://config.xml', "/var/www/{$projectname}/config.xml");
It seems that variable $projectname not converted to its value because you used single quotes.
I am getting the error:
PHP Fatal error: Class 'Symfony\\Component\\Process\\PhpProcess' not found in ...
It's my first time using Symfony. I download the files from the GitHub site. Since the downloaded files are of the Process directory, I created folders in order to have the correct path. (Symfony/Component/Process/DOWNLOADED FILES).
I placed the Symfony directory in the main directory (where my index file is).
Is this how you install Symfony? (Note I only need the Process sub-directory and not the whole framework)
For now I am using this ready-made code, just to see if I can get it running:
use Symfony\Component\Process\PhpProcess;
$command = file_get_contents('/hello_world.php');
if ( $command ) {
$process = new PhpProcess($command); //<----- GETTING ERROR HERE
$process->run();
if ($process->isSuccessful()) {
$output = $process->getOutput();
// ...
}
else {
throw new Exception($process->getErrorOutput());
}
}
Why am I getting this error and how can I fix it please? Is it due to a bad path?
I think it has to do with namespace registration, have you checked that the namespace that defines the class is loaded?, check the ClassLoader reference
Check the autoload.php file inside your app directory
I'm trying to upgrade the charts in my zend framework based website to the open flash chart 2 (version: kvasir) library. It used to work just fine with older versions. However, using the new version, when I try to create e.g. lines, I get error messages such as
Warning: include(line\hollow.php) [function.include]: failed to open stream: No such file or directory in C:\xampp\htdocs\weMatch\library\Zend\Loader.php on line 83
Fatal error: Class 'line_hollow' not found in C:\xampp\htdocs\weMatch\application\default\controllers\FeedbackController.php on line 215
I suspect that the Zend_Loader_Autoloader is replacing the underscore with a backslash. Can I exclude the whole open flash chart library from the Zend_Loader_Autoloader?
It is changing the _ to /. You could just include the file yourself beforehand.
require_once('path/to/line_hollow.php'); or whatever the filename is.
I found out that the API of the open flash library has changed with the new version (kvasir). As in old versions, there's a file you need to include which includes all the other library classes (open-flash-chart.php). I saw that the class line_hollow.php was commented out in this file and I therefore guess it's deprecated. When I tried to include line_hollow.php, there were some follow up errors. Luckily, there are other ways to achieve the same result. Instead of:
$line = new line_hollow();
you go
$line= new line();
$dot = new hollow_dot();
$line->set_default_dot_style($dotTeam);
The latter way gives you more flexibility, because you can give the $dot additional attributes.