Rectangle 27 4

Working with file paths and Phar archives

Working with file paths and Phar archives in PHP can be tricky. The PHP code inside of a Phar file will treat relative paths as being relative to the Phar archive, not relative to the current working directory. Here's a short example:

Say you have the following files:

phar/index.php
test.php
my.phar

The index.php file is located inside of the phar directory. It is the bootstrap file for the phar archive:

function does_it_exist($file){
  return file_exists($file) ? "true" : "false";
}

The bootstrap file is executed when the phar file is included from a PHP script. Our bootstrap file will simply cause the function "does_it_exist" to be declared.

Let's try running different code inside of test.php and see what the results are for each run:

//Run 1:
require_once 'phar/index.php';  //PHP file
$file = __DIR__ . "/index.php"; //absolute path
echo does_it_exist($file);      //prints "false"

//Run 2:
require_once 'phar/index.php';  //PHP file
$file = "index.php";            //relative path
echo does_it_exist($file);      //prints "false"

//Run 3:
require_once 'my.phar';         //phar file
$file = __DIR__ . "/index.php"; //absolute path
echo does_it_exist($file);      //prints "false"

//Run 4:
require_once 'my.phar';         //phar file
$file = "index.php";            //relative path
echo does_it_exist($file);      //prints "true"

Look at Run 4. This code includes the phar file and passes the function a relative path. Relative to the current working directory, index.php does not exist. But relative to the contents of the phar archive, it does exist, which is why it prints "true"!

i know, i know. thats not the question. my phar package creates a new file OUTSIDE of the phar script using fwrite. that works perfectly, a new file is created. and this new can later be read by include or require but it cant be "seen" by file_exists - which is my problem - i think it might be a bug in phar

thanks, but if include('login.php') or require('login.php') both find and read the file, than surely if(file_exists('login.php')) would return true - surely by the fact that its read by include/require shows the file path/location is correct, and so that is not the issue?

hey thanks man, i think i'm starting to see what your saying. But my problem is that WITHIN my phar file, i want to check for a file OUTSIDE of it. when i run my phar file it creates 'login.php' in the same directory as the phar, but outside of it. then when you run the phar again, i need it to check if that login.php is there, outside. what file path do i use for that?

include - PHP Phar - file_exists() issue - Stack Overflow

php include require file-exists phar
Rectangle 27 9

The PHAR of PHPUnit can only be properly used as a library as of PHPUnit 4.2.

I'm attempting to use PHPCOV but it doesn't appear to like the .cov files generated by phpunit 3.7.31. Can you confirm if phpcov will work with .cov files from phpunit 3.7.31?

No, PHPCOV does not support PHPUnit 3.7. You need PHPUnit 4 for PHPCOV to work properly. Please note that PHPUnit 3.7 is not actively supported by the PHPUnit project anymore.

php - How to properly require a phar file - Stack Overflow

php phpunit require phar
Rectangle 27 24

Phar allows you to put an entire PHP application into a PHP Archive. It is not a direct executable as you may assume.

To install Laravel 4.1 successfully on Windows 7, you need Composer -a package manager. First install Composer. This will install globally on your system. Composer can now be called through your command prompt via 'composer'.

Next, go to where your WAMP or XAMP project folder is -generally, this would be your www folder (i.e. C:\wamp\www).

Make a new project directory: www\new_project. Now go to your start menu and run cmd as admin. Next you need to change your directory to the C drive and then into your www\new_project folder:

C:\> cd wamp\www\new_project

Now you can take advantage of composer by calling:

composer create-project laravel/laravel --prefer-dist

Call the above statement in that new_project folder because that is where laravel will install. The above will make your directory path as:

C:\wamp\www\new_project\laravel\

Laravel is now available on your system. You can verify a successful install by going to:

http://localhost/new_project/laravel/public/

Based on the above question edit regarding making a .phar globally available for command:

The directory your looking for is C:\bin --the equivalent folder to /usr/bin on Linux. Copy the laravel.phar file in the C:\bin folder. Or , you can put it in a folder, such as, C:\php\laravel.phar. Then you need to make a batch file somewhere within the PATH called laravel.bat which will then do the following:

@ECHO OFF
php "%~dp0laravel.phar" %*

The "%*" repeats all of the arguments passed to the shell script. Thus, you can run 'laravel new project'. Hope this points you in the right direction.

Thanks for you answer, but I'm specifically looking for the way of installation that is labeled "Via Laravel Installer" in the documentation, since it provides some benefits over the installation via composer. Basically what I want is, that I can start a new project by typing laravel new [name] in the command line.

Laravel uses composer to manage dependencies. Dependencies that update and change over time. What benefits are available over not installing via composer?

My Apologies. My Docs was set to 4.0 and not 4.1. Once you have laravel.phar file, just copy that over onto your project folder or www directory: C:/wamp/www/new_project/laravel.phar. By default, laravel.phar needs to be ran by PHP.exe on your cmd inside your project folder. To do so, you need to adjust your PATH variable -verify that you did it correctly: stackoverflow.com/questions/12870880/ Afterwards, you need to call "php laravel.phar new project"

Thanks, that worked if the laravel.phar file is in the same folder. But how do I go about this, if I want the laravel command to be globally available? I added the folder where the laravel.phar file is located to my path ( in my case C:\Windows\System32\laravel ), restarted the command line, but it does not work ( Could not open input file: laravel.phar ). The documentation suggests to move it into the usr/local/bin folder which obviously is not available on windows systems.

php - Installing Laravel 4.1 in Windows 7 // Make .phar file globally ...

php command-line windows-7 laravel-4 phar
Rectangle 27 6

At the PHAR's stub, you can use the __DIR__ magic constant to get the PHAR file's folder.

With that in mind, you can simply use

is_file(__DIR__ . DIRECTORY_SEPARATOR . $path);

To check for a file's existence outside the PHAR.

You can ONLY do this from the stub, and ONLY if it's a custom stub, as opposed to one generated by Phar::setDefaultStub(). If you need to check for files further down the line, you'll have to make that constant's value available somehow, like a global variable, a custom non-magical constant or a static property or something, which other files then consult with.

EDIT: Actually, you can also use dirname(Phar::running(false)) to get the PHAR's folder from anywhere in the PHAR. That function returns an empty string if you're not within a PHAR, so whether your application is executed as a PHAR or directly, it should work fine, e.g.

$pharFile = Phar::running(false);
is_file(('' === $pharFile ? '' : dirname($pharFile) . DIRECTORY_SEPARATOR) . $path)

include - PHP Phar - file_exists() issue - Stack Overflow

php include require file-exists phar
Rectangle 27 4

I meet the same issue today. After several hours digging ... I found the answer.

Can you try the following script first?

if(file_exists(realpath('file.php')));

If the file exist, then the issue is

if you use only the filename without path information, php treat file is related to phar stub. for example:

phar:///a/b/c/file.php

So, you have to use absolute path to manipulate the file, for example:

/home/www/d/e/f/file.php

include - PHP Phar - file_exists() issue - Stack Overflow

php include require file-exists phar
Rectangle 27 5

The documentation on the Laravel website is not a good way to install laravel on windows. You'll got problem with the routing later.

http://localhost/new_project/laravel/public/

to get a better URL you must setup Apache Virtual Host and edit hosts file.

The best way to install Laravel on windows is to use Git and Composer. if you already successfully install Git and Composer, open Git bash and using ls and cd terminal command go to c:\xampp\htdocs folder

and run this command (it will ask you for your Git passphrase, make sure you install your Git properly - tutorial here - http://www.thegeekstuff.com/2012/02/git-for-windows/):

git clone git@github.com:laravel/laravel.git laraveldev

It will download laravel into a folder name laraveldev in htdocs:

c:\xampp\htdocs\laraveldev

Use the Git bash terminal to install laravel into PHP using this command:

composer install
c:\windows\system32\drivers\etc
127.0.0.1             www.laravel.dev

and put virtual hosts entry in c:\xampp\apache\conf\extra\httpd-vhosts.conf.

<VirtualHost *:80>
    DocumentRoot "C:/xampp/htdocs/laraveldev/public"
    ServerName www.laravel.dev
    ServerAlias www.laravel.dev
    ErrorLog "logs/laravel.log"
    CustomLog "logs/custom.laravel.log" combined
    <Directory "C:/xampp/htdocs/laraveldev/public">
        AllowOverride All
        Order Allow,Deny
        Allow from all
        Require all granted
    </Directory>
</VirtualHost>

Restart you xampp apache. Then you can access laravel app in your browser like this:

I'm totally 100% sure you'll got those "You have arrived" text :D

Hi, I am new to Laravel, composer etc and I installed using method mentioned by @KLVTZ. What if I follow your advice! Will it conflict etc? More please.

This instruction is the only one that worked for me to get ride off the ugly URL. So yes, it work 100%

If you use helper url's you won't have problems with routing without pretty url's.

php - Installing Laravel 4.1 in Windows 7 // Make .phar file globally ...

php command-line windows-7 laravel-4 phar
Rectangle 27 2

I've seen the following used to exclude git directories using a negative lookahead:

$phar->buildFromDirectory(__DIR__, '/^((?!\.git).)*$/');

php - phar exclude directories when creating tar archive - Stack Overf...

php directory tar phar
Rectangle 27 2

The problem with your regex is that the full path of the file gets matched, not only the directory (basename). Thus you cannot filter out the .git directory itself.

Using a character class negation ([^.][^g][^i][^t]) does also not help because there are parts of the path that do match this regex, so the path matches anyway.

This means you can use positive matches only.

You could use Phar::buildFromIterator and then use RecursiveFilterIterator to filter out the files. There you can define your own matching method that filters correctly.

php - phar exclude directories when creating tar archive - Stack Overf...

php directory tar phar
Rectangle 27 3

installing laravel is easy way with composer, if you cant use composer, than you can go with laravel.phar file. This method is also easiest way to install laravel on your local machine.

The link in this answer helped. But I had to run as an admin user in the command prompt to get it working.

php - Installing Laravel 4.1 in Windows 7 // Make .phar file globally ...

php command-line windows-7 laravel-4 phar
Rectangle 27 2

file_exists in php has several issues, in particular cases.

I recommend you to use stat() instead, which is commonly preffered way to obtain that info, not just in php. Also don't forget to clear the cache by calling clearstatcache().

function FileExists($filename)
{
    clearstatcache(false, $filename);

    return false !== @stat($filename);
}

Note : I try to avoid the suppress operator @ as much as I can but I see this particular case as neccessary to use it :)

include - PHP Phar - file_exists() issue - Stack Overflow

php include require file-exists phar
Rectangle 27 2

I can reproduce this behavior by calling Phar::interceptFileFuncs(). It seems fopen calls in write mode are not intercepted, while stat-related functions are.

Since absolute filenames are treated as related to the filesystem even in Phar archives that intercept file functions:

[...] Absolute paths are assumed to be attempts to load external files from the filesystem.

realpath()
if (file_exists(realpath($filename)) { /* file exists on filesystem */ }

include - PHP Phar - file_exists() issue - Stack Overflow

php include require file-exists phar
Rectangle 27 1

Not sure what is causing file_exists() to fail but you can try something like this

function fileExists($path){
    //try to open the file, if it can be read the file exist
    return (@fopen($path,"r") == true); 
 }

Yes ofcourse! You can close it afterwards if the fopen function succeed. Open the file, if it succeed close the file and return true, else return false.

include - PHP Phar - file_exists() issue - Stack Overflow

php include require file-exists phar
Rectangle 27 1

ZIP of some Phar Lap manuals (including a 386|ASM one) at:

c - Phar Lap Assembler: I Need information/documentation and binaries ...

c legacy dos assembly
Rectangle 27 4

Use MAMP's PHP in the Terminal

Here are the steps I used to successfully get PHPUnit working in MAMP. These instructions are pieced together from various places. I hope that having it all in one place helps someone else. Happy testing!

Edit or create ~/.bash_profile with the lines below

# Use MAMP's latest version of PHP
MAMP_LATEST_PHP=`ls /Applications/MAMP/bin/php/ | sort -n | tail -1`
export PATH=/Applications/MAMP/bin/php/${MAMP_LATEST_PHP}/bin:$PATH

Place these lines after any other lines exporting $PATH - this assures that your MAMP PHP is found first in the path. Note that these lines try to find the highest numbered version of PHP in your MAMP installation. Feel free to adjust this to a specific one that you have, if desired.

You can tell you did it right when you get a MAMP path from which php in your terminal. You should get something like this:

/Applications/MAMP/bin/php/php7.0.0/bin/php

Mostly, this is downloading the PHP archive (PHAR) from the PHPUnit website. There are ways to do this from the command line that I couldn't get to work. So, I used a web browser.

  • Download the most recent PHPUnit PHAR from https://phar.phpunit.de
/usr/local/bin
cd /usr/local/bin
chmod +x phpunit-5.3.2.phar
  • Make a symbolic link with ln -s phpunit-5.3.2.phar ./phpunit (adjust according to actual name)
phpunit -version

You should get something like this:

PHPUnit 5.3.2 by Sebastian Bergmann and contributors.

Building a symbolic link in step 5 permits you to use phpunit instead of having to type phpunit-5.3.2.phar instead. It also allows you to update PHPUnit without having to change what you type, assuming of course that you create a new symbolic link when you update.

This isn't an exhaustive section. There are far better tutorials on writing tests. Instead, this is merely some notes from my experience on rules that tripped me up, though I'm sure everyone else knows them:

class SomeTest extends PHPUnit_Framework_TestCase
  • Your test class file name must end with Test.php and match the contained class: SomeTest.php
  • Method names in your test class that are to be run as tests must start with test: public function testSomething()

By this time, it should be as easy as:

phpunit SomeTest

If everything goes well, PHPUnit will run your test and give you the results.

Assuming that it all works (Yay!) try this alias in your ~/.bash_profile

# Use colors when running phpunit
alias phpunit='phpunit --colors'

One line you forgot to mention after adding the lines to the bash file. "Use source ~/.bash_profile after making your changes to make sure they take effect."

php - How to configure PHPUnit in MAMP with PHAR - Stack Overflow

php unit-testing phpunit
Rectangle 27 3

Value 127 is returned by /bin/sh when the given command is not found within your PATH system variable and it is not a built-in shell command.

Try using exec('php composer.phar', $out, $ret); and see if that works. You might also need to use the full path to php if its in a non-standard location which you can probably get from which php.

Im not sure why you are using passthru here. I would use exec for better handling. Id use exec here instead of passthru

class Maintenance_Controller
{

    public function do_maintenance()
    {
        exec("composer.phar update", $out, $ret);
        if(!$ret) {
            // handle success
        } else {
           // handle error
        }

    }
}

This way you have all the output by line in $out as well as the shell return val (0 if ok, > 0 if not). If you wanna get really fancy you can loop over $out and scan for the errors and then build and exception to throw.

exec()

Did you try with the full path to php instead of executing through env?

I think you can use PHP_BINDIR for the full path. Not sure which versions that covers though.

php - Call a .phar executable from inside a web app controller - Stack...

php composer-php webhooks
Rectangle 27 1

/usr/local/bin/ is just recommended as a convention, because it's always in the $PATH on Unix systems, you can use any other location that's in your path, or leave it anywhere else and specify the absolute path when using it.

With what you have, you should be able to run:

/Applications/MAMP/bin/php/php5.2.17/bin/phpunit.phar --version

Note the .phar suffix, because you did not rename the file while moving, as you'd have with sudo mv phpunit.phar /usr/local/bin/phpunit

php - How to configure PHPUnit in MAMP with PHAR - Stack Overflow

php unit-testing phpunit
Rectangle 27 202

$ curl -sS https://getcomposer.org/installer | php
$ mv composer.phar /usr/local/bin/composer

Then it's likely that you, like me, ran those commands and didn't read the next part of the page telling you to stop referring to composer.phar by its full name and abbreviate it as an executable (that you just renamed with the mv command). So this:

$ php composer.phar update friendsofsymfony/elastica-bundle
$ composer update friendsofsymfony/elastica-bundle

The thing to remember is that if you installed it correctly (on Windows) and added it to your environment's PATH variable then you do not need to type php composer.phar and use composer instead.

php - Running Composer returns: "Could not open input file: composer.p...

php symfony wamp composer-php
Rectangle 27 202

$ curl -sS https://getcomposer.org/installer | php
$ mv composer.phar /usr/local/bin/composer

Then it's likely that you, like me, ran those commands and didn't read the next part of the page telling you to stop referring to composer.phar by its full name and abbreviate it as an executable (that you just renamed with the mv command). So this:

$ php composer.phar update friendsofsymfony/elastica-bundle
$ composer update friendsofsymfony/elastica-bundle

The thing to remember is that if you installed it correctly (on Windows) and added it to your environment's PATH variable then you do not need to type php composer.phar and use composer instead.

php - Running Composer returns: "Could not open input file: composer.p...

php symfony wamp composer-php
Rectangle 27 106

I had the same problem on Windows and used a different solution. I used the Composer_Setup.exe installation file supplied by the composer website and it does a global install.

After installing, make sure your PATH variable points to the directory where composer.phar is stored. This is usually C:\ProgramData\ComposerSetup\bin (ProgramData might be a hidden directory). It goes without saying, but also be sure that the PHP executable is also in your PATH variable.

You can then simply call

composer install

instead of

php composer.phar install

This finally works, I don't understand why I can't find this info anywhere on the composer website ... .

php - Running Composer returns: "Could not open input file: composer.p...

php symfony wamp composer-php
Rectangle 27 106

I had the same problem on Windows and used a different solution. I used the Composer_Setup.exe installation file supplied by the composer website and it does a global install.

After installing, make sure your PATH variable points to the directory where composer.phar is stored. This is usually C:\ProgramData\ComposerSetup\bin (ProgramData might be a hidden directory). It goes without saying, but also be sure that the PHP executable is also in your PATH variable.

You can then simply call

composer install

instead of

php composer.phar install

This finally works, I don't understand why I can't find this info anywhere on the composer website ... .

php - Running Composer returns: "Could not open input file: composer.p...

php symfony wamp composer-php