• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

PHP exec() Not Working on a WAMP Install

morgrar

Limp Gawd
Joined
Feb 28, 2005
Messages
249
Perhaps it's a security issue. Perhaps it's something else, but for the life of me, I haven't been able to get a PHP script working with a batch file I've created on an XP machine running WAMP.

Here's the scoop. I need to obtain the newest .csv files from a remote SFTP every day and then perform certain functions on these files and import them over to their respective MySQL tables on a database.

I have an old-school DOS batch file created that will create a directory with the current date, move the existing .csv files into said directory, and then open up winscp or psftp to log onto the SFTP and obtain the new .csv files. These files have several different names and then random time/datestamps trailing in their file names.

My script for both PSFTP or WinSCP basically instructs the program to log on and get/mget *.csv. Upon successful retrieval of these .csv files, they are renamed to a format that my other scripts will read. All of this is handled in my batch file, let's call it script.bat.

If I run script.bat from a command prompt in Windows, everything runs beautifully. What I've been trying to do is get this to work on a PHP page so any one in my intranet can run this from a web browser and it'll perform said functions on the server and do it automatically.

My PHP script is in the same directory as the script.bat and I'd access it from the browser. The code just calls upon the script.bat, and in this fashion, all the DOS commands work fine, but the winscp/psftp does not work correctly (I mention both, because I'll take whichever one ends up working). I do not receive any error messages in the PHP error log, or the Apache error log. I've even tried procmon to find anything with an ACCESS DENIED result, but have been unable to find anything there as well. If I'm monitoring the Task Manager when the script is run from a browser, I see a process of winscp.exe or psftp.exe open up but not increase in memory size or cpu allocation, and it just sits there as an open/held process.

Code:
<?php
exec("script.bat");
?>

Any ideas what I'm doing wrong or what I need to look for? I've read everything on php.net I've found in terms of accessing exec() functions on Windows. I've also tried system(), shell_exec(), etc.

I hope whoever reads this was able to follow my explanation of said plight. Thanks in advance.
 
If any of those programs are Win32 or GUI programs, that might be your problem. I don't think you can exec non-command-line programs via PHP. One thing to try would be to shell_exec a .bat file that tries to simply open calc.exe

psftp is a command-line program, though, correct? hmm...

My guess is that this is a permissions issue. Find out what User / Group the Apache process is running under and make sure it has permission to run those programs. Another thing to try would be actually copying the program executables to your script directory and manually setting the file owner to the same one that Apache is running under. (Sorry if I'm off base, I'm more used to PHP under *NIX)

Another thing you can try is dumping the result of your shell_exec. If you did
Code:
<?php echo shell_exec("script.bat"); ?>
it would dump any error output that the program was generating as it launched. That might help explain why they open silently and remain as held processes.

Let me know if you figure this out, or what comes up. I'm interested in what you find.
 
If any of those programs are Win32 or GUI programs, that might be your problem. I don't think you can exec non-command-line programs via PHP. One thing to try would be to shell_exec a .bat file that tries to simply open calc.exe

psftp is a command-line program, though, correct? hmm...

My guess is that this is a permissions issue. Find out what User / Group the Apache process is running under and make sure it has permission to run those programs. Another thing to try would be actually copying the program executables to your script directory and manually setting the file owner to the same one that Apache is running under. (Sorry if I'm off base, I'm more used to PHP under *NIX)

Another thing you can try is dumping the result of your shell_exec. If you did
Code:
<?php echo shell_exec("script.bat"); ?>
it would dump any error output that the program was generating as it launched. That might help explain why they open silently and remain as held processes.

Let me know if you figure this out, or what comes up. I'm interested in what you find.

Correct, psftp is command-line based. I checked permissions to the SYSTEM user, as well as IUSR. Even for the sake of testing, I enabled full control the IUSR user as per the other things I've read on php.net and other forums.

I attempted at performing the echo prior to the shell_exec, and it didn't produce any results, but still had an open process of psftp.exe in the Windows Task Manager on the server. The actual executables are also placed in the same directory as the PHP scripts and batch files.

I'm at a loss. It's so frustrating since I can run the entire psftp.exe command from a command line prompt and it works so well.
 
Why not just create a scheduled task to run your batch file every 5 minutes or so?
 
My guess is that, when you're running the batch file interactively, your environment has the location of psftp in the PATH but, when running under the webserver, you don't so it doesn't know where to find the executable & fails.

You probably want to either modify the environment or provide the full path to the .exe's when you call them.
 
My guess is that, when you're running the batch file interactively, your environment has the location of psftp in the PATH but, when running under the webserver, you don't so it doesn't know where to find the executable & fails.

You probably want to either modify the environment or provide the full path to the .exe's when you call them.

I've tried specifying the full path as well, and even including the actual exe files within the same directory as the php and dos scripts. None of this seemed to work. I even installed the PECL win32std library extension to see if that would help, and tried it with windows_shell_exec, but that didn't work either.

I ended up setting a Scheduled Task to connect and download the newest files every morning before anyone gets into the office. The new files are generally available by then. If not, then it's something the end-users will just deal with. (It's fine.)
 
Back
Top