Creating a PHP script from BASH code to run on Windows - php

I have this line of code:
convert 1234_Page_1_....png 1234_Page_2_....png output.pdf
This merges the listed pngs to a single pdf (using ImageMagick). I have a bunch of PNG files in this format. I would like to create a loop in PHP to run through the convert/merge action on all the files that I have. For all the files that have the same number before the "Page", I would like them to be merged to one PDF with that same number as a name. Sometimes there are more than two pages to convert.
For this example, I would like "1234_Page_1_....png" and "1234_Page_2_....png" to result as 1234.pdf. And I would like files "1235_Page_1_....png" and "1235_Page_2_....png" to result as 1235.pdf, and so on.
Here's what I've been told is the BASH way to handle the problem:
for i in `seq 1234 1350` ; do convert ${i}_Page_*.png ${i}_output.pdf ; done
I would like to have this done in a PHP script that I can run on Windows.
Thanks in advance,
Jake

The reason you're getting that error message is that you edited the script using a Windows editor and then tried to run it in a Unix-type environment. Run dos2unix scriptname and the Windows line endings will be converted to Unix line endings. No more $'\r' errors. Also, in Bash use for i in {1234..1350} - there's no need to use seq.

Related

Using popen() to upload ardiuno sketch

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.)

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

Creating a Log Detector: Running Shell Scripts from PHP not working. Processes keep running OR broken pipe

My aim is to create a log file reader using PHP and Shell scripting.
I plan to run the script at intervals in order to detect the last time a particular entry shows up in the logfile.
The way it should work is by calling the PHP script which in turn calls a shell command
tac logfile.log | grep "what i am looking for" | head -n 1
or
tac logfile.log | head -n 1
What this script will do is:
in the case of the first script: scan the log file from the bottom up until it finds what it is looking for and shows me the first line, which is actually the last occurrence of the line in the file.
in the case of the second script: scan only the last line and output it. I am NOT trying to tail the file.
So my intention is to scan the file from the end to the last occurence but I need php to do this because PHP can parse the lines it reads from the shell output and parse them easily.
The issue i am trying to resolve is that once I run the script from PHP using the functions shell_exec, exec, backtick operator, system etc. They all keep the PHP script running or output the result and keeps the first part of the shell running (tac logfile.log).
I dont understand why it works perfectly from CLI but runs indefinitely by trying to tac the whole log file when I run the same script from PHP. Ive noticed that tac and cat give this problem. Sometimes it shows the error "Broken pipe".
What should I be doing? How can I fix this?
You could use pure PHP. Open the the file with fopen, an example how to read a file backwards can be found at Read a file backwards line by line using fseek
than just break the loop after you found your string you search for.

imagemagick convert does not work through php exec() but works through command line

When I try to convert pdf to php via command line:
convert 1.pdf 2.jpg
it works and gives me jpg image. But then I try to convert via php exec() it doesn't work.
exec("convert 1.pdf 2.jpg");
Process starts but I haven't result. I try to use
putenv("PATH=/usr/local/bin:/usr/bin:/bin")
and full path to file but it doesn't help.
I have a new detail of my problem: when I run my php script via command line - it works. But when I try to run it in my browser process starts and works a few seconds (~15 sec) but I haven't result.
I also had a development where I needed to convert pdf to image. I also used imagemagick. In my case, my web hoster has disabled the PHP function exec() for security reason. Is it possible that you have the same problem?
So, I was not able to use PHP to run imagemagick. My web hoster asks me to use a Pearl script and then it was working.
I hope my remark can help you.
Try using the PECL library for ImageMagick. Gives you a lot of options using PHP to work with imagemagick
http://php.net/manual/en/book.imagick.php

php command not found

I am using Php in Linux Cent OS. I wanted to convert PDF to SWF, So I use SWFTools. SWFTools consists of pdf2swf command, which is executing fine when I use it in command line. But the command is not working when I execute it through php.
Error: command not found
I have found the extensions are up to date.
Anything else is missing in between?
Use the full path.
The path environmental variable will be different when running under PHP, since that will run as another user.

Categories