|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2017-07-28 06:09 UTC] marco dot agnoli at me dot com
Description:
------------
Some functions like chdir() raise an error when they fail, but others like getcwd() don't.
I don't see why getcwd() should not raise an error.
This makes catching errors more painful than it should be.
Test script:
---------------
<?php
var_dump(getcwd());
// Remove traversal permission for the current directory
system('chmod -x '.escapeshellarg(__DIR__));
/**
* I would expect an error here
* because getcwd() returns false here.
*/
var_dump(getcwd());
// Restore traversal permission
system('chmod +x '.escapeshellarg(__DIR__));
?>
Expected result:
----------------
A warning from PHP that getcwd() failed.
Actual result:
--------------
getcwd() silently returns false.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Nov 02 18:00:01 2025 UTC |
Same can be said for the `realpath` function: ``` <?php error_reporting(E_ALL); var_dump(realpath('non-existing-path')); ?> ``` realpath() expects a valid path, so why not raise an error when no valid path was specified?I have thought about this issue once again and actually getcwd can do some serious damage if used incorrectly. I'm aware that it is unlikely for getcwd() to fail but consider the following piece of code: <?php $path = \getcwd().'/bin/'; system('rm -rf '.\escapeshellarg($path)); ?> Since PHP converts false to an empty string the folder we attempt to remove would be "/bin" and we wouldn't even know about it!