I have a PHP application running in the Linux subsystem for Windows.
The application talks to an external .exe file that requires a filename as an input.
In my PHP application, I need to determine if the file exists before trying to send it to the external .exe file.
To summarise, when in my PHP application I need the Linux file path, but when passing it as an argument to the .exe I need the windows file path.
So it would look something like this:
<?php
$fn = '/mnt/c/myapp/myfile.png';
$exists = file_exists($fn);
if ($exists) {
shell_exec('external.exe -f ' . transformToWindowsPath($fn));
}
function transformToWindowsPath($fn)
{
// What should go here?
// Is something like this reliable?
return str_replace(
'/',
DIRECTORY_SEPARATOR,
preg_replace_callback(
'/\/mnt\/([a-zA-Z])\//',
function($matches) {
return strtoupper($matches[1]) . ":" . DIRECTORY_SEPARATOR;
},
$fn
)
);
}
Try with this function that converts the directory separator:
$fn = '/mnt/c/myapp/myfile.png';
function fnSystem($fn, $system='linux')
{
if($system == 'windows')
{
$fn = str_replace('/mnt/c/', '', $fn); // remove /mnt/c/ from front
$fn = str_replace('/', '\\', $fn); // convert the directory separator
$fn = "C://$fn"; // place C:// in front
}
elseif($system == 'linux')
{
$fn = str_replace('C://', '', $fn); // remove C:// from front
$fn = str_replace('\\', '/', $fn); // convert the directory separator
$fn = "/mnt/c/$fn"; // place /mnt/c/ in front
}
return $fn;
}
$fnWindows = fnSystem('/mnt/c/myapp/myfile.png', 'windows');
echo $fnWindows;
Result:
C://myapp\myfile.png
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.