Search code examples
phpfilefile-exists

how to check if .exe file exists in php


Hi there is there any way to check if .exe file exists on a given path or not. I have installation of ImageMagic. I have a path of convert.exe of Image Magic. I need to check that in given path the convert.exe exists or not. I have implemented

$settingFileContent = file_get_contents($settingFilePath);
       // print_r($settingFileContent);
        $allPaths = unserialize(stripslashes($settingFileContent));

if (isset($allPaths['IMAGE_CONVERT_EXE'])) {
                //cho $allPaths['IMAGE_CONVERT_EXE'];
                if (file_exists($allPaths['IMAGE_CONVERT_EXE'])) {
                    $analysisResultObj->level = ENUM_SUCCESS;
                } else {
                    $analysisResultObj->level = ENUM_ERROR;
                    $analysisResultObj->infoText = "Image Magic convert.ext has wrong path";   
                 Logger::getLogger('Application')->error('Image Magic convert.ext has wrong path');
                }
            }

I can change the value of $allPaths['IMAGE_CONVERT_EXE'] in file. When I change to wrong value even in that condition it returns true.


Solution

  • Based on the documentation comment specifically about PHP on Windows I'm guessing (and let's be clear: everything in PHP is a guess) try this:

    $file = 'd:/somfolder/imagemagic/convert.ext'
    if(file_exists($file)) {
        // should be false
    }
    

    Based on your actual code have you tried:

    $file = $allPaths['IMAGE_CONVERT_EXE'];
    if(file_exists($file)) {
        // should be false
    }
    

    Looking at the documentation someone commented about having this same problem on Windows and being unable to return the correct result when concatenating string values. While you are not concatenating string values together its at least worth a shot to make sure there isn't something else strange going on.