Using popen() to upload ardiuno sketch - php

I am trying to use PHP to compile and upload an Arduino sketch through the command line. Right now a user uploads an ino or pde file through a form and it is transferred to a directory for later use. Using the uploaded file's location as a variable, I would like PHP to run the command line version of Ardunio to compile and upload it.
After an inital try with using exec() and system(), I switched to popen(). Running the following code I can get Arduino to open then it closes without uploading the sketch:
pclose(popen('"C:\Program Files\Arduino\arduino.exe" --port COM3 --upload "C:\sketches\uploads\cube\a\a.ino"));
Running that code and its variations through the Windows Command Line works so I know the input string is not the issue. Also, looking at the Windows Task Manager shows it opening for a second or so then closing. Could someone point me in the right direction?

For popen (or any of the other process functions) to work correctly on Windows you need to escape backslashes like this:
pclose(popen('"C:\\Program Files\\Arduino\\arduino.exe" --port COM3 --upload "C:\\sketches\\uploads\\cube\\a\\a.ino"'));
alternatively try replacing the backslashes with forward slashes. The following should also work on recent versions of Windows:
pclose(popen('"C:/Program Files/Arduino/arduino.exe" --port COM3 --upload "C:/sketches/uploads/cube/a/a.ino"'));
(Your code snippet was also missing a trailing single quote, but I suspect that's a typo.)

Related

Executing Scripts via PHP

I am trying to execute a script file (Batch or Python) in PHP (local WAMP server) that will open a program on my computer and send a keyboard shortcut to put the the program in fullscreen mode. I have this script already made using AutoHotKey (.ahk scripts).
I tried using these PHP commands to open a Batch file that runs the .ahk script:
system("cmd /c C:\wamp64\www\test.bat");
exec("test.bat");
exec("cmd.exe /c test.bat");
But all of these seem to just run the script on the webserver and not on my Windows computer so the .ahk file is never executed. I also tried directly executing .ahk file but I couldn't get that working either.
Does anyone know of a way I can use PHP(or another web language) to execute this script on my computer?
You have to specify absolute path for the batch file, as the CLI SAPI has nothing to do with WAMP's document root.
You have tried to pass full path in the first command. But the sequence \t within double quotes is parsed as a tabulation character. Change double quotes to single quotes:
system('cmd /c C:\wamp64\www\test.bat');
Also, I don't think you need to run cmd explicitly, since batch files are executable on Windows.

Printing Word document from PHP in WAMP

So, I have this printer that doesn't have drivers for any OS other than Windows, so, I wanted to run a server on a Windows PC that is attached to the printer, so that I can upload a file from any device in the local network and have a PHP script invoke MS Word on the file to print it. The command I have to invoke Word to print a document works when I give it from the command line and the file upload works, but when the command is run from the PHP script with system() or exec(), it does not work. I see WINWORD.exe running from the Task Manager, but with no GUI showing and no printing happening. I am running the latest WAMP on Windows 8 and I have tried going into services.msc and changing the logon for wampapache to my user which is an administrator or enabling the checkbox to allow the service to interact with the desktop, but none of that worked. This is the actual command I am using: "C:\Program Files\Microsoft Office\Office14\WINWORD.EXE" C:\path\file.txt /q /n /mFilePrintDefault /mFileExit. It's in a batch file that I run using exec()
EDIT: I am not trying to print to a network printer, this is a printer that is connected to the server computer and I am using the newest version of WAMP, so I can't use the PHP functions in the linked question. Also, I am trying to print Word documents not raw text. This is specifically about PHP's exec() not working for this case.
I honestly think this is your best bet:
How To Make Microsoft Word Documents with PHP
Since you seem to be having problems with the .bat file approach, I'd suggest trying a Powershell script instead. For example:
print_doc.ps1 =>
Param([string] $filePath)
# This should handle any file type, including an MS-Word .doc/.docx
Start-Process -FilePath $filePath -Verb print
In PHP, you can exec() the script something like this:
'powershell.exe -noprofile -executionpolicy bypass -file \path\to\script\print_doc.ps1 "' . $path . '"'
STRONG SUGGESTIONS:
Make sure the path is valid INSIDE OF PHP.
You can use file_exists() and/or realpath() to verify this.
Enable verbose error logging. In Powershell, I like to use try/catch blocks and the $error object.
Good luck!

PHP exec giving error while the command runs successfully in bat file

I am running a local WAMP on my Windows 7 with a PHP script that executes a windows command as follows:
`exec('"%CD%\files_for_redistribution\ppt2html5.exe" /i:"%CD%\test.ppt" /o:"%CD%\output.html" /title:title /desc:description /author:author /keywords:keywords',$output,$error);`
The command when run from a batch file does the job well but when run from PHP script, gives an error:Presentation opening error: PowerPoint could not open the file.
The intention of the command is to convert PowerPoint to HTML using a third party software called ppt2html5.exe where test.ppt has to be converted to output.html.
I have found lot of blogs discussing about exec function not working properly but nothing really helped me to deal with this error as it runs the command but cannot open the file.
It would be great if somebody could help me with this.
Check if safe mode is on, because that activates escapeshellcmd and some characters are escaped.
Assuming that the string that you are passing to exec(), including percentage signs, routes and parameters are right, your problem may be related to permission of files and user executing apache + php, check that.
Fixed by adding a folder named Desktop inside C:\Windows\System32\config\systemprofile.
Source:http://www.sitepoint.com/forums/showthread.php?956457-Windows-2008-PHP-new-COM%28powerpoint-application%29

Using PHP shell_exec to print PDF on the network automatically to a network printer not working

I'm using shell_exec() to print a PDF that is stored on a LAN automatically to a printer on the LAN through Adobe Reader using the following PHP code (all on one line):
shell_exec("'C:\Program Files\Adobe\Reader 10.0\Reader\AcroRd32.exe'
/t \\hnurensp01\RepairTickets\179694.pdf \\hnurenfp01\Accounts_FS-1128MFP");
What I expect this to do is open adobe reader, open the PDF on the network and print to the network printer. When running the PHP script with this line, it doesn't open adobe reader or print it. However, when I copy that code and execute in a command prompt (client-side), it works and prints. There is only slight modifications to the command line (all on one line):
"C:\Program Files\Adobe\Reader 10.0\Reader\AcroRd32.exe"
/t \\hnurensp01\RepairTickets\179694.pdf \\hnurenfp01\Accounts_FS-1128MFP
Is it to do with the PHP script executing on the server-side? I've tried running the same command line on the server and it works too. It just seems to be when it's going through PHP, it does nothing.
My end result must be printing out a server-side PDF with as little interaction from the end-user as possible. Note: this is a WAMP environment.
Any ideas?
Thanks.
UPDATE:
Tried put the code into a batch file and calling it that way, still didn't work. Tried using exec() and shell_exec() but still no joy. For some reason when I run the batch file by double-clicking it, it works fine. It's when I call it using PHP, it doesn't do anything except open two instances of acrord32.exe that doesn't close and therefore the PHP script seems to never complete. I'm soo confused...
Try this:
echo "'C:\Program Files\Adobe\Reader 10.0\Reader\AcroRd32.exe' /t \\hnurensp01\RepairTickets\179694.pdf \\hnurenfp01\Accounts_FS-1128MFP"
It returns:
'C:\Program Files\Adobe\Reader 10.0\Reader\AcroRd32.exe' /t \hnurensp01\RepairTickets9694.pdf \hnurenfp01\Accounts_FS-1128MFP
In PHP, the backslash serves as a 'special' character, since it's used to produce tabs and newlines \t and \n. The proper way to escape a backslash, is by prepending another one: \\ produces \ as output.
This is why your PHP script doesn't work. The double backslashes will be printed to the shell as if you only typed one! You will need to escape every backslash in that line of code:
shell_exec("'C:\\Program Files\\Adobe\\Reader 10.0\\Reader\\AcroRd32.exe'
/t \\\\hnurensp01\\RepairTickets\\179694.pdf \\\\hnurenfp01\\Accounts_FS-1128MFP");

PHP exec() with CUDA

exec("fun.exe input/input.txt ");
I want to run an CUDA program in PHP,
the task is:
load data from an input.txt. (argument)
calculating.
write an output.txt.
and PHP read ouput.txt to do next task.
In server1(Apache ,Windows XP), it can run perfectly,
but in server2,3(Apache, Windows 7),the output is wrong.
The program doesn't crash and there's no any error message in the page,
it seems like something wrong during the execution.
Next I try exec the All CPU-side version (same calculation),server2,3 can run correctly.
If I exec the fun.exe(CUDA version) in server2,3 directly(double click or in command line),the program also run perfectly.
Any idea on why server2,3 can't run the program? Thanks.
First, try using the full path to the executable. Then the full path to the input file too.
If that doesn't work, then try modifying the file permissions (try with full 777 permissions, if that works then you know where your problem lies).
Try to use the entire path (windows version using backslash).

Categories