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 8756883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:10:32+00:00 2026-06-13T14:10:32+00:00

Possible Duplicate: php exec command (or similar) to not wait for result exec() waiting

  • 0

Possible Duplicate:
php exec command (or similar) to not wait for result
exec() waiting for a response in PHP

I have a php script that calls and runs a Matlab script. The result of the Matlab script is a .png image, which I would then like to load in php and send to a webpage. The php code I have is:

$matlabExe = '"C:\\Program Files\\MATLAB\\R2012a\\bin\\matlab.exe"';
$mFile = "'C:\\processSatData.m'";
$combine = '"run(' . $mFile . ');"';
$command = $matlabExe . ' -nodisplay -nosplash -nodesktop -r ' . $combine;

passthru($command);

$im = file_get_contents('C:\\habitat.png');
header('Content-type:image/png');
echo $im;

However, it appears that after sending the ‘passthru’ command, php does not wait for the Matlab script to finish running. Thus, if the image file does not exist before running the php code, then I get an error message.

Is there a way to make it so that the php code waits for the Matlab script to finish running before it attempts to load the image file?

  • 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-06-13T14:10:34+00:00Added an answer on June 13, 2026 at 2:10 pm

    passthru is not the main issue here .. but i guess as soon you have a response from your command the image is not written instantly but by a 3rd process

    file_get_contents might also fail in this instance because .. The image might not be written once or in the process of writing which can result to file lock .. in any case you need to be sure you have a valid image before output is sent;

    set_time_limit(0);
    $timeout = 30; // sec
    $output = 'C:\\habitat.png';
    $matlabExe = '"C:\\Program Files\\MATLAB\\R2012a\\bin\\matlab.exe"';
    $mFile = "'C:\\processSatData.m'";
    $combine = '"run(' . $mFile . ');"';
    $command = $matlabExe . ' -nodisplay -nosplash -nodesktop -r ' . $combine;
    
    try {
        if (! @unlink($output) && is_file($output))
            throw new Exception("Unable to remove old file");
    
        passthru($command);
    
        $start = time();
        while ( true ) {
            // Check if file is readable
            if (is_file($output) && is_readable($output)) {
                $img = @imagecreatefrompng($output);
                // Check if Math Lab is has finished writing to image
                if ($img !== false) {
                    header('Content-type:image/png');
                    imagepng($img);
                    break;
                }
            }
    
            // Check Timeout
            if ((time() - $start) > $timeout) {
                throw new Exception("Timeout Reached");
                break;
            }
        }
    } catch ( Exception $e ) {
        echo $e->getMessage();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: php exec command (or similar) to not wait for result I have
Possible Duplicate: PHP - send file to user I have written a script that
Possible Duplicate: php false place in condition I have noticed that a lot of
Possible Duplicate: Run a PHP CLI script from a webpage I have a problem
Possible Duplicate: php in background exec() function After a certain script runs on website
Possible Duplicate: Build Tar file from directory in PHP without exec/passthru Does PHP have
Possible Duplicate: php shell_exec() vs exec() How do I run a linux command from
Possible Duplicate: PHP image replacement? I'm writing an edit script that allows a row
Possible Duplicate: PHP call class in class returns error:500 I have code that looks
Possible Duplicate: PHP Get end string on url between / and / I have

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.