php / php/php-src

realpath() and SplFileInfo::getRealPath inside Phar

Open
#14,481 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Extension: phar Status: Verified
Dominant language
C
Stars
40.4k
Forks
8.1k
Avg merge
2d 13h
Merged PRs (30d)
96

Description

Description

I'm trying to include packages in my phar but many of them use realpath to files inside the Phar which spits out false instead of a path; breaking things. If the behavior was to return the same path when the path starts with phar:// it would just work.

The following code:

make_phar.php

<?php
$phar = new Phar('test.phar');
$phar->addFile('test.php');
$phar->setDefaultStub('test.php');
$phar->stopBuffering();

test.php

<?php
var_dump(
    __FILE__,
    realpath(__FILE__), 
    (new SplFileInfo(__FILE__))->getRealPath(),
);

php test.php && php make_phar.php && php test.phar

Resulted in this output:

string(45) "/home/murrant/projects/realpath_test/test.php"
string(45) "/home/murrant/projects/realpath_test/test.php"
string(45) "/home/murrant/projects/realpath_test/test.php"
string(62) "phar:///home/murrant/projects/realpath_test/test.phar/test.php"
bool(false)
bool(false)

But I expected this output instead:

string(45) "/home/murrant/projects/realpath_test/test.php"
string(45) "/home/murrant/projects/realpath_test/test.php"
string(45) "/home/murrant/projects/realpath_test/test.php"
string(62) "phar:///home/murrant/projects/realpath_test/test.phar/test.php"
string(62) "phar:///home/murrant/projects/realpath_test/test.phar/test.php"
string(62) "phar:///home/murrant/projects/realpath_test/test.phar/test.php"
PHP Version

PHP 8.3.7

Operating System

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running make_phar.php, test.php, and the provided php test.phar command to reproduce the false results for realpath() and SplFileInfo::getRealPath(). Trace the PHP and Phar filesystem handling involved in those calls. Done means both functions return the phar:// path shown in the expected output, with the existing non-Phar results unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.