Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3399430
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:46:13+00:00 2026-05-18T04:46:13+00:00

I’ve written a web application that makes use of exec() in order to run

  • 0

I’ve written a web application that makes use of exec() in order to run an external program. The program path is configurable and can be expected to have spaces on its name. As we all know, the Windows command prompt accepts spaces in file names or parameteres, you just have to double quote them:

C:\>C:\Archivos de programa\GraphicsMagick-1.3.12-Q16\gm.exe
"C:\Archivos" no se reconoce como un comando interno o externo,
programa o archivo por lotes ejecutable.

C:\>"C:\Archivos de programa\GraphicsMagick-1.3.12-Q16\gm.exe"
GraphicsMagick 1.3.12 2010-03-08 Q16 http://www.GraphicsMagick.org/

So far so good. The issue I’m facing is the usage of the exec() PHP function itself. Certain Windows servers require that you enclose the full command (program + arguments) in double quotes:

exec('""C:\Archivos de programa\GraphicsMagick-1.3.12-Q16\gm.exe" version"');

… and other Windows servers require not to use double quotes:

exec('"C:\Archivos de programa\GraphicsMagick-1.3.12-Q16\gm.exe" version');

I can read the PHP_OS constant to detect whether the server runs Windows but I don’t know what’s the rule behind the quotes or not quotes subject. It if’s explained in the PHP manual I cannot find it.

Is is possible to determine programmatically if quotes are needed so I don’t need to configure manually each instance of the application?

Update #1: I was being misunderstood so I’ve reworded parts of the question to make it more clear.

Update #2: I found a comment in the PHP manual that explains the exact reason why extra quotes are needed (PHP issues an internal call to cmd /c). I still don’t know why this appears to be true or false depending on the system.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T04:46:13+00:00Added an answer on May 18, 2026 at 4:46 am

    I’ve written this quick and dirty workaround:

    <?php
    
    class Thumb{
        const GM_PATH = 'C:\\Archivos de programa\\GraphicsMagick-1.3.12-Q16\\gm.exe';
    
        /**
         * Quote full command if required by server (program + arguments)
         */
        private static function quoteFullCommand($command){
            // Test only once per script
            static $extra_quotes_required=NULL;
    
            if( is_null($extra_quotes_required) ){
                if(PHP_OS=='WINNT'){
                    // This call will be correct (0) if and only if the server requires extra quotes
                    exec('""sort" /?"', $output, $return);
                    $extra_quotes_required = $return==0;
                }else{
                    $extra_quotes_required = FALSE;
                }
            }
            if($extra_quotes_required){
                $command = '"' . $command . '"';
            }
    
            return $command;
        }
    
    
        /**
         * Return output from "gm version"
         */
        public static function graphicsMagickVersion(){
            $command = escapeshellarg(self::GM_PATH) . ' version ';
            $command = self::quoteFullCommand($command);
            exec($command, $output, $return);
    
            return trim(implode(PHP_EOL, $output));
        }
    }
    

    However, it’d be way better to predict it from PHP version or server OS so links to documentation or further tips are welcome.

    Update: I’ve had a look at the piece of PHP source code that takes care of running external commands in Windows:

    http://svn.php.net/viewvc/php/php-src/trunk/TSRM/tsrm_win32.c

    The following line adds the extra comas to the full command:

    sprintf(cmd, "%s /c \"%s\"", TWG(comspec), command); 
    

    According to the file history, that line was first added on 29th May 2008 (r260429):

    MFH: Fix bug when command is quoted
    and parameters are quoted during call
    to exec, the result is that cmd.exe /c
    strips the first and last quote.

    The following PHP releases were 5.3.0 and 5.2.7 but the line is in the 5_3 branch and not in the 5_2 one. I’m not familiar enough with PHP development process so I can’t find the changelog or tell out to which exact PHP releases the fix was ported to but I’d dare say that it was a bug in PHP and it was fixed in PHP/5.3.0 (yet it wasn’t backported to 5.2 so they didn’t break legacy stuff).

    So my workaround is probably overkill. You just need to test the PHP OS and version:

    if( PHP_OS=='WINNT' && version_compare(PHP_VERSION, '5.3.0', '<') ){
       $command = $command = '"' . $command . '"';
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.