19

I am actually using wamp 2.5 with PHP 5.5.12 and when I try to create a phar file it returns me the following message :

Uncaught exception 'UnexpectedValueException' with message 'creating archive "..." disabled by the php.ini setting phar.readonly'

even if I turn to off the phar.readonly option in php.ini.

So how can I enable the creation of phar files ?

user3292788
  • 407
  • 1
  • 6
  • 15
  • 3
    Make sure that you are editing the correct **php.ini**. You can use `phpinfo()` to find the loaded php.ini – randrade86 Jan 08 '16 at 00:40
  • You're totally right I think that I posted too quicky. I was effectively editing the wrong php.ini file. – user3292788 Jan 08 '16 at 00:43
  • 3
    I've got the exact same issue running PHP Version 5.5.9-1ubuntu4.14, however, I have verified that I am editing the correct php.ini as defined in `phpinfo()`, in my case it's `/etc/php5/apache2/php.ini` and the line reads `phar.readonly = Off` Is there anything else that needs to be enabled? – Jeff Puckett Apr 10 '16 at 03:34
  • In scripts executed in command line, the file of folder cli must be edited, for example: /etc/php5/cli/php.ini – alditis Aug 15 '17 at 23:59
  • Hi, @user3292788 if any of below answers has solved your question please consider accepting it by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – Vineet Jain Feb 05 '19 at 07:11
  • There was no main answer that solved my problem at that time but what suggested **mimimito** was the correct way to get the right **php.ini** file. There is btw one below that is marked now. – user3292788 Feb 09 '19 at 00:38

6 Answers6

14

I had this same problem and pieced together from info on this thread, here's what I did in over-simplified explanation:

  1. in my PHP code that's generating this error, I added echo phpinfo(); (which displays a large table with all sort of PHP info) and in the first few rows verify the path of the php.ini file to make sure you're editing the correct php.ini.
  2. locate on the phpinfo() table where it says phar.readonly and note that it is On.
  3. open the php.ini file from step 1 and search for phar.readonly. Mine is on line 995 and reads ;phar.readonly = On
  4. Change this line to phar.readonly = Off. Be sure that there is no semi-colon at the beginning of the line.
  5. Restart your server
  6. Confirm that you're phar project is now working as expected, and/or search on the phpinfo()table again to see that the phar.readonly setting has changed.
  • On Linux use: `php -i | grep phar.readonly` to validate if it is on. And `php -i | grep "Loaded Configuration"` to find the current configuration. Then `sudo nano thefile`, CTRL+W `phar.readonly` (enter). Change the line and save with CTRL+X, Y, (enter) – Wesley Abbenhuis May 27 '19 at 08:16
5

phar.readonly can only be disabled in php.ini due to security reasons. If you want to check that it's is really not done using other method than php.ini then in terminal type this:-

$ php -r "ini_set('phar.readonly',0);print(ini_get('phar.readonly'));" 

If it will give you 1 means phar.readonly is On.
More on phar.configuration

Vineet Jain
  • 1,515
  • 4
  • 21
  • 31
  • 1
    as @NVRM [pointed out](https://stackoverflow.com/a/65061895/9618184), it CAN be changed without chaging the `php.ini` file. Example: `php -d phar.readonly=0 -r "print(ini_get('phar.readonly'));"` – Binar Web Apr 02 '22 at 13:28
3

Using php-cli and a hashbang, we can set it on the fly without messing with the ini file.


testphar.php

#!/usr/bin/php -d phar.readonly=0
<?php
print(ini_get('phar.readonly')); // Must return 0
// make sure it doesn't exist
@unlink('brandnewphar.phar');
try {
    $p = new Phar(dirname(__FILE__) . '/brandnewphar.phar', 0, 'brandnewphar.phar');
} catch (Exception $e) {
    echo 'Could not create phar:', $e;
}
echo 'The new phar has ' . $p->count() . " entries\n";
$p->startBuffering();
$p['file.txt'] = 'hi';
$p['file2.txt'] = 'there';
$p['file2.txt']->compress(Phar::GZ);
$p['file3.txt'] = 'babyface';
$p['file3.txt']->setMetadata(42);
$p->setStub('<?php
function __autoload($class)
{
    include "phar://myphar.phar/" . str_replace("_", "/", $class) . ".php";
}
Phar::mapPhar("myphar.phar");
include "phar://myphar.phar/startup.php";
__HALT_COMPILER();');
$p->stopBuffering();

// Test
$m = file_get_contents("phar://brandnewphar.phar/file2.txt");
$m = explode("\n",$m);
var_dump($m);
/* Output:
* there
**/

✓ Must be set executable:

chmod +x testphar.php

✓ Must be called like this:

./testphar.php
// OUTPUT there

⚠️ Must not be called like this:

php testphar.php
// Exception, phar is read only...

⚠️ Won't work called from a CGI web server

php -S localhost:8785 testphar.php 
// Exception, phar is read only...
NVRM
  • 11,480
  • 1
  • 88
  • 87
2

Need to disable in php.ini file

Type which php Gives a different output depending on machine e.g. /c/Apps/php/php-7.2.11/php Then open the path given not the php file.

E.g. /c/Apps/php/php-7.2.11

Edit the php.ini file could do

vi C:\Apps\php\php-7.2.11\php.ini

code C:\Apps\php\php-7.2.11\php.ini

[Phar]
; http://php.net/phar.readonly
phar.readonly = Off

; http://php.net/phar.require-hash
phar.require_hash = Off

Save

lastlink
  • 1,505
  • 2
  • 19
  • 29
1

For anyone who has changed the php.ini file, but just doesn't see any changes. Try to use the CLI version of the file. For me, it was in /etc/php/7.4/cli/php.ini

ALZlper
  • 61
  • 1
  • 9
0

Quick Solution!

Check:

cat /etc/php/7.4/apache2/php.ini | grep phar.readonly

Fix:

sed -i 's/;phar.readonly = On/;phar.readonly = Off/g' /etc/php/7.4/apache2/php.ini
Peter Csala
  • 17,736
  • 16
  • 35
  • 75
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 06 '21 at 01:22