Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP detect (or remove) current drive letter?

Tags:

php

windows

I'm trying to find a way for PHP to detect the drive letter of the USB my app is on, but I'm not having any luck.

I'm making a desktop which goes hand in hand with the apps I installed from portableapps.com. The main desktop and all the apps are on a USB drive; the PHP files are stored in the USB version of xampp.

Currently I have a conditional statement set up where if I was to click a desktop icon for one of the portable apps, it would append "?app=APPNAME" which would then be picked up by the conditional and open the app.

<?php 
$app = $_GET['app'];

if ($app == 'pidgin') {
 $addr = "E:/PortableApps/PidginPortable/PidginPortable.exe";
 exec ($addr,$output, $return);
} 
?>

I want to be able to plug this drive into any computer and not run into problems opening the apps, so is there a way to remove the E:/ in the file location and still have them open or at least a way to detect what drive letter the USB is using and change that part of my code based on what it detects?

like image 755
Jason Avatar asked Jan 22 '12 17:01

Jason


2 Answers

You may be able to parse it from the __FILE__ magic constant. I don't have any Windows computer, but I guess it will be the first character. So this may work:

$drive = substr(__FILE__, 0, 1);
like image 114
Emil Vikström Avatar answered Oct 21 '22 10:10

Emil Vikström


// Returns null if unable to determine drive letter (such as on a *nix box)
function driveLetter($path)
{
    return (preg_match('/^[A-Z]:/i', $path = realpath($path))) ? $path[0] : null;
}

// To find drive letter of current file
echo "Drive letter is: ", driveLetter(__FILE__);
like image 20
Unsigned Avatar answered Oct 21 '22 12:10

Unsigned