Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an application which was developed using php and I want to print a dithering image through my printer so what I am doing is I have an application called ImageMagick. In this application using command prompt I will pass an image path and the destination folder. So when I run the command in the command prompt it will convert the image and save it in destination folder.

What I have tried:

Firstly I tried the php exec() for the below code
<pre><pre lang="PHP"><pre><?php
     echo exec('ipconfig');
    ?>

The above code gives the output on the browser

When I give the below code:
<pre lang="PHP"><pre><?php
     echo exec('date');
   ?>

It gives the output as below
PHP
Enter the new date: (mm-dd-yy)

But When I run the date command on the command prompt it gives the current date
I tried to run the Imagemagick command in php code as shown below
<pre lang="PHP"><pre><?php
echo exec('convert "C:\bhargav\dev\download.jpg" -resize 384 -dither FloydSteinberg -remap pattern:gray50 "C:\bhargav\dev\con.jpg"');
?>

But it doesn't save any image as output and no error is shown on the browser.
convert "C:\bhargav\dev\download.jpg" -resize 384 -dither FloydSteinberg -remap pattern:gray50 "C:\bhargav\dev\con.jpg"

when the above command is given in the command prompt it gives me the output.

Where am I going wrong?
Posted
Updated 14-Mar-19 6:58am
v2
Comments
Richard MacCutchan 17-Feb-19 9:02am    
"Where am I going wrong?"
Short answer: everywhere.
Longer answer: Browsers are specifically restricted from executing applications on client systems for security reasons. Any attempt to do what you are trying is likely to fail.
Kornfeld Eliyahu Peter 17-Feb-19 9:21am    
Doesn't save? How do you know? have you checked the C: drive of the server? Or you were thinking that PHP is running on the client machine?
Dave Kreskowiak 17-Feb-19 10:09am    
PHP code runs on the server, not the client. All the stuff you're launching with 'exec' runs only on the server.

Now if you're thinking "but it works on my machine!". Yeah, that's because YOUR machine is both the server AND the client at the same time! Deploy something to a production server and everything you launch with 'exec' will only ever run on the server.

1 solution

Any errors would likely be going to stderr, not stdout. I suspect php exec() only captures stdout content (by default, if otherwise). You need to look at the server error log, not the browser, to see error messages from exec()'d programs.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900