Fix finfo_file() for remote resources issue
#20759
Closed
+108
−7
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix finfo_file() for remote resources (GH-20679)
Summary
Fixes
finfo_file()to work correctly with remote URLs (HTTP/HTTPS) by ensuring thatmagic_stream()is called even whenphp_stream_stat()fails on remote resources.Problem
Previously,
finfo_file()would returnfalsefor remote URLs because:php_stream_stat()fails on remote streams (stat is not available for HTTP/HTTPS resources)magic_stream()inside theif (php_stream_stat() == SUCCESS)blockret_valremainedNULL, causing the function to returnfalseExample of the bug:
Solution
Restructured the logic in
php_fileinfo_from_path()to:php_stream_stat()only to detect directories (when stat succeeds)magic_stream()when the resource is not a directory, regardless of whether stat succeeded or failedThis ensures that:
magic_stream()even when stat failsChanges
Code Changes
File:
ext/fileinfo/fileinfo.cBefore:
After:
Test Case
File:
ext/fileinfo/tests/finfo_file_remote_url.phptAdded a new test case that verifies
finfo_file()works with remote HTTP resources using a local test server.Testing
Manual Testing
Tested with various scenarios:
Remote HTTP URL:
Remote HTTPS URL (requires OpenSSL):
Local files: Still work correctly
Directories: Still correctly identified as "directory"
Automated Testing
Run the test suite:
Result: ✅ PASS
Requirements
For Testing
pcntl extension: Required to run the PHPT test (uses
http_server()which requirespcntl_fork())./configure --enable-pcntlallow_url_fopen: Must be enabled (set in test's
--INI--section)For HTTPS Support (Optional)
./configure --with-opensslBackward Compatibility
✅ Fully backward compatible:
falseRelated Issues
Additional Notes
magic_stream()which works for all stream types