Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't enable phar writing

Tags:

php-ini

phar

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 ?

like image 294
user3292788 Avatar asked Jan 08 '16 00:01

user3292788


5 Answers

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.
like image 166
DriveItLikeYouStoleIt Avatar answered Nov 16 '22 03:11

DriveItLikeYouStoleIt


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...
like image 24
NVRM Avatar answered Nov 16 '22 03:11

NVRM


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

like image 4
Vineet Jain Avatar answered Nov 16 '22 03:11

Vineet Jain


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

like image 2
lastlink Avatar answered Nov 16 '22 04:11

lastlink


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

like image 1
ALZlper Avatar answered Nov 16 '22 04:11

ALZlper