PHP 8.1.2 realpath returning false - php

I am currently upgrading my PHP website from 7.4 to 8.1.2 but I'm having an issue with 'realpath'. No matter what directory I put in the variable '$install_folder' just shows as false. It's bizarre as I tested this on my PHP 7.4 website and the code works as it should.
$install_folder = realpath(__DIR__ . '/../../../');
I have tried to cheese the fix by:
$dir = __DIR__;
$install_folder = realpath($dir . '/../../../');
Any suggestions would be highly appreciated!

Is the php cli interpreter installed on your system? On Debian/Ubuntu based systems this is the phpX-cli package.
IF the cli package is installed, I'd want to see the output of a test script that might shed some light on things.
#/path/to/php8
$dir = __DIR__;
echo $dir;
$install_folder = realpath($dir);
echo $install_folder;
Run this script as a regular user, and then as the user running the web site.

Related

I can get PHP code to unzip a file running on Linux, but not on Windows 10

I am not entirely sure that this is coding problem, so I'll describe my Windows environment, just in case: system is windows 10 Pro x64 build 1809, I have XAMPP windows x64 7.2.18-0-VC15 installed. On XAMPP I have PHP Version 7.2.18.
Following PHP code executes OK when I try it on Linux (took it from Stackoverflow)
:
function unzip_db($filename) {
$zip = new ZipArchive;
$res = $zip->open($filename);
if($res === TRUE) {
$zip->extractTo('./');
$zip->close();
echo 'File is now uncompressed...';
} else {
echo "Can't open ".$filename;
echo $res;
}
}
When I try to run it under Windows this code always fails on the test if($res === TRUE) I am now not sure what to do: I check the output of phpinfo(); is says about ZIP: Zip enabled, Zip version: 1.15.4, Libzip version: 1.4.0, that could mean that I PHP have ZIP support and I am doing something wrong. I have also located and downloaded php_zip.dll (from https://pecl.php.net/package/zip/1.15.4/windows) and added line extension=php_zip.dllto php.ini file, but nothing has changed.
I ran out of ideas and I still don't know do I have a coding or environment config trouble, or what...
Any suggestions?
We can close this question. I have found that after I've zipped the test files and uploaded that to Linux, I downloaded them back to Windows box. It is that files creating the issue with PHP. It is kind of strange that I can still open them with 7zip, but I cant open them with Windows's built in unzipper.
Even when I upload that files back to Linux, I can still unzip them with PHP. Seems there is difference between Linux and Windows implementation of ZIPlibraries, but that is not the reason for the problems.
So I have to troubleshoot what happens when up/downloading the files - but that is out of scope of this question.
Thanks for your contribution. It helped to set me straight, so I was able to determine wherein lays the trouble.

Php move files strange bug

I'm facing a strange bug that I can't find words to describe !
When I use move_uploaded_file function with this path "/Project/Uploads/Imgs/"
When I use the same function with this path "/opt/lampp/htdocs/Project/Uploads/Imgs/"
It works normally !
My code
$avatar = $_FILES['avatar'];
$avatarName = filter_var($avatar['name'], FILTER_SANITIZE_SPECIAL_CHARS);
$avatarTmp = $avatar['tmp_name'];
$randomAvatar = rand(0,1000000000) . "_" . $avatarName;
move_uploaded_file($avatarTmp, $uploads . $randomAvatar);
While Variable $uploads = the path
Another bug,
Every time PHP moves the file into the uploads folder, I've to make chown from the linux terminal with sudo permissions to make it readable
I've solved it
Basically, I just had to change the path from /Project/Uploads/Imgs to ../Uploads/Imgs
Well, At first this wasn't working.
But, I discovered that it was a bug from chromium browser or something, cause when I've restarted the browser, and changed the path, It worked fine !
The Second Bug also was a bug from the browser
I don't know how, but The second bug was fixed after restarting the browser.

Gettext not working through php-cli, but works in php-apache

The code i run looks as the following:
...
$this->locale = da_DK;
...
putenv("LC_ALL=".$this->locale);
putenv('LANG='.$this->locale);
$res = setlocale(LC_ALL, $this->locale);
if($res != $this->locale){
throw new Exception("The language could not be set.");
}
bindtextdomain("domain", "./locale");
textdomain("domain");
echo setlocale(LC_MESSAGES, 0 );
echo ' ';
echo __('description');
Result running it through apache2 is:
da_DK Beskrivelse
Which is as we want.
Running it throgh cli:
da_DK Description
Which would have been correct if we had used english. The 'd' is upper case in the english translation and the lower case in the source (From out of context it seems a bit weird)
So gettext works as it can translate the string, but somehow it disregards that i changed locale in the script and chose the .po file from the english directory.
In case you wonder why i need locales on a cli script: The script is used to emails out invoices.
I use:
Ubuntu 12.10, PHP 5.4.6-1ubuntu1, apache 2.2.22, and gettext 0.18.1.
You have to reset the environment Variable LANGUAGE:
putenv("LANGUAGE=");
Took me a while to find out.
You need to check if the bindtextdomain() points the same resource file from php-cli and php-apache. I suggest,
echo the return value of the bindtextdomain() and see them.
try to replace "./locale" to absolute path (e.g. "/var/www/yourproject/locale") and see if it works.
I don't know how much this is relevant or not but …
I just found a solution to the opposite problem: Gettext not working through php-apache, but working in php-cli.
Also concerned, strftime localization not working through php-apache but working in php-cli (on a Linux Debian buster system).
LANGUAGE environment variable did not help as I do not have such environment variable.
Strangely, I just disabled the perl module in Apache and the problem disappeared.
So maybe some bugs in any Apache module could trigger this kind of problem…

How to configure wampserver for spatialite

I am in windows 7. I installed WAMPServer. Now, I can not load spatialite libraries. It is showing warning..
Warning: SQLite3::loadExtension() [sqlite3.loadextension]: Not
supported in multithreaded Web servers
Here is my total configuration procedure, what I have done...
From this link I got spatialite lib. I copied "libspatialite-1.dll" and paste it to "D:\wamp\bin\php\php5.3.8\ext" which contains php extention dlls.
http://www.gaia-gis.it/spatialite-2.3.1/libspatialite-win-x86-2.3.1.zip
Then I edited php.ini file. I changed the following configuration.
.
sqlite3.extension_dir = C:\libspatialite-win-x86-2.3.1\bin
.
extension=libspatialite-1.dll
.
enable_dl = On
And lastly, I copy and paste all the libraries from my downloaded libspatialite-win-x86-2.3.1 to my project folder(libspatialite.a, libspatialite.dll.a, libspatialite.la) in my php code i write the script as follows...
<?php
$db = new SQLite3('sixcommunes.sqlite');
$db->loadExtension('libspatialite.a');
$rs = $db->query('SELECT spatialite_version()');
while($row = $rs->fetchArray()){
print "<h3>SQLite version: $row[0]</h3>";
}
?>
I do not know what I have done wrong or how to solve this problem?
Haven't seen any answer, so giving my own. I have used spatialite in C#, I think for using Spatialite Extensions you don't have to load libspatialite like a usual PHP extension, instead something similar to this should be done. (this is c# code, just trying to give you an idea)
SQLiteCommand sqliteCommand = new SQLiteCommand(String.Format("SELECT
load_extension('{0}');", "libspatialite-2.dll"), connection);
sqliteCommand.ExecuteNonQuery();
You have to execute Select load_extension("libspatialite-2.dll") on sqlite to use Spatialite.
Hope this helps

How can I get the path of the PHP binary from PHP?

How can I get the binary path of php from PHP?
I saw it in phpinfo(), but I need another method that gets it in Linux and Windows systems.
You can use:
$_SERVER['_']
Also, the predefined constant PHP_BINDIR gives the directory where the PHP executable is found.
Sample on CodePad and Ideone.
It looks like, for security reasons, $_SERVER values are not exposed.
Linux Only
Use the "which" command to find php.
$phpPath = exec("which php");
Note this does not guarantee the same php executable that your web server may be using, but rather the first instance that was found while looking through the paths.
A method using environment variables, assuming the php executable is in the system path.
function getPHPExecutableFromPath() {
$paths = explode(PATH_SEPARATOR, getenv('PATH'));
foreach ($paths as $path) {
// We need this for XAMPP (Windows)
if (strstr($path, 'php.exe') && isset($_SERVER["WINDIR"]) && file_exists($path) && is_file($path)) {
return $path;
}
else {
$php_executable = $path . DIRECTORY_SEPARATOR . "php" . (isset($_SERVER["WINDIR"]) ? ".exe" : "");
if (file_exists($php_executable) && is_file($php_executable)) {
return $php_executable;
}
}
}
return FALSE; // Not found
}
Maybe the best solution is in the Symfony process component:
PhpExecutableFinder.php and ExecutableFinder.php. In use:
<?php
use Symfony\Component\Process\PhpExecutableFinder;
$phpFinder = new PhpExecutableFinder;
if (!$phpPath = $phpFinder->find()) {
throw new \Exception('The php executable could not be found, add it to your PATH environment variable and try again');
}
return $phpPath;
In Windows, using WAMP, you can use the ini variable - extension_dir - as it is placed in the PHP folder.
Like this:
echo str_replace('ext/', 'php.exe', ini_get('extension_dir'));
Normally, in a simple default PHP installation under Windows, the php.ini file is located and loaded from the same directory of the PHP binary.
To simplify, Windows users:
echo dirname(php_ini_loaded_file()).DIRECTORY_SEPARATOR.'php.exe';
Voilà!
Of course, if you are using multiple .ini files, it may not work if the files are not into the same PHP binary directory. BTW, this may solve to most of cases. Windows developers running PHP from local development environment.
As of PHP 5.4 you can simply use the PHP_BINARY reserved constant.
It's very easy!
var_dump(getenv('PHPBIN'));
But it works only on Windows, so we should use this answer.
How did I get this? I just typed echo echo phpinfo(); and searched the php path there. Just see here:
Then I just getting it here: php getenv and ... you see the result.
For Windows and XAMPP:
$php = getenv('PHPRC') . '/php.exe';
if(is_file($expected)){
return $php;
}

Categories