...

View Full Version : Problems getting PHP to run Linux system commands



nuebelhor
10-21-2009, 04:34 AM
Hello all,

Trying to get PHP script to execute Linux commands...the same commands worked using a shell script on Linux machine. But when I try to execute the same commands in the PHP script I get the following error:

couldn't get a file descriptor referring to the console

Here is the PHP code:



<php
$remote_file = file.txt

system("ftp -nv <<EOF");
system("open 'ftp.etl.noaa.gov'");
system("user 'user_name' 'passwd'");
system("cd et7/data");
system("mget $remote_file");
system("y");
system("EOF");


?>


Any help is greatly appreciated! Thanks,

Nick

Fou-Lu
10-21-2009, 04:42 AM
PHP has a builtin ftp library that may be of more use, I wouldn't use system / exec when you don't need to.
Try using an absolute file path on you're filename, and also be certain to wrap it in quotes.
BTW, my suspicion is simply that it can't find the file relative to the executing script. I would presume that the system interface takes the cwd out of PHP, which would make it from the executing location. Also, it will inherit the permissions of you're apache use (nobody, apache, whatever), so make certain that apache can read it as well (assuming apache module in use).

oesxyl
10-21-2009, 04:50 AM
Hello all,

Trying to get PHP script to execute Linux commands...the same commands worked using a shell script on Linux machine. But when I try to execute the same commands in the PHP script I get the following error:

couldn't get a file descriptor referring to the console

Here is the PHP code:



<php
$remote_file = file.txt

system("ftp -nv <<EOF");
system("open 'ftp.etl.noaa.gov'");
system("user 'user_name' 'passwd'");
system("cd et7/data");
system("mget $remote_file");
system("y");
system("EOF");


?>


Any help is greatly appreciated! Thanks,

Nick
write a shell script who take as argument that filename and call the script from php.
From my point of view this is the wrong way to do such things, first because you can do that only with a shell script or if you want only with php. I don't see any reason to mix them.

best regards

oesxyl
10-21-2009, 04:58 AM
PHP has a builtin ftp library that may be of more use, I wouldn't use system / exec when you don't need to.
Try using an absolute file path on you're filename, and also be certain to wrap it in quotes.
BTW, my suspicion is simply that it can't find the file relative to the executing script. I would presume that the system interface takes the cwd out of PHP, which would make it from the executing location. Also, it will inherit the permissions of you're apache use (nobody, apache, whatever), so make certain that apache can read it as well (assuming apache module in use).
I was thinking to something else that break that script:



<php
$remote_file = file.txt

system("ftp -nv <<EOF"); // <-- open a file descriptor and wait for EOF?
system("open 'ftp.etl.noaa.gov'"); // <- this try to open another file descriptor, or I'm wrong?
system("user 'user_name' 'passwd'"); // <- another one?
system("cd et7/data");
system("mget $remote_file"); // <- +1
system("y"); // <- this is a short form for 'yes' probably but, ... +1
system("EOF"); // <- who get this EOF?, +1 :)

?>

I guess that after first system, anything is useless, including my comments, :)

best regards

nuebelhor
10-21-2009, 06:32 AM
Thanks for the comments,

Do not have access to the computer now...but the file, file.txt, changes every hour, so I already have a script written in PHP to come up with the actual name of file at that specific hour. If there is some way to change the file name in the shell script I could do it that way...but as of now I don't know a way to do it using a shell script. More comments appreciated, thanks,

Nick

oesxyl
10-21-2009, 07:02 AM
Thanks for the comments,

Do not have access to the computer now...but the file, file.txt, changes every hour, so I already have a script written in PHP to come up with the actual name of file at that specific hour. If there is some way to change the file name in the shell script I could do it that way...but as of now I don't know a way to do it using a shell script. More comments appreciated, thanks,

Nick
I don't understand do you have or not a shell script who work with a given filename, no matter what name?
Let's say your shell script is 'myshellscript.sh', then you can call it from php:



$remote_file = "file.txt";
system("./myshellscript.sh $remote_file");


inside the shell script you must have something like that:


txtfile=$1

and somewere inside you use $txtfile shell variable.
I guess you don't need to change the filename, maybe I'm wrong, depends of what is in your code.

a better way, suggested by Fou-Lu, is to use php ftp module:
http://www.php.net/manual/en/book.ftp.php

this way you don't need any shell script.
I don't know how your shell script looks but as far as I know this is a job for expect not bash/sh since you must wait for a ftp connection and provide authentification data.

best regards

nuebelhor
10-21-2009, 06:59 PM
Thanks I will try that.

nuebelhor
10-22-2009, 03:59 AM
Thanks oesxyl that worked!

oesxyl
10-22-2009, 02:28 PM
Thanks oesxyl that worked!
you are welcome. :) What solution did you choose, with shell script or with all code in php using ftp module?

best regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum