views:

43

answers:

3

Hello,

I have a unix command written in a file and I need PHP to read it and execute it. The reason it needs to be read from a file is because the commands symbols mess up the script when put them directly in it. So far I have this:

<?php
$command = readfile("http://localhost/command.txt");
echo shell_exec($command);
?> 

Upon running this all I get is the value of the text file and the command is not executing. Is there a way this can be done?

Thanks for any help

A: 

I think you are over-complicating matters with this approach. Try to put the string in your php script directly, and show what goes wrong.

mvds
+2  A: 

The readfile() function does not do what you think it does. It outputs the contents of the file to the browser, and returns the number of bytes read.

Instead, you want file_get_contents():

$command = file_get_contents('http://localhost/command.txt');
echo shell_exec($command);

But one question: Why are you pulling the command file from a website? Why not the local files-ystem?

ircmaxell
I could do the local filesystem to neither ones better (to my knowledge). Just used localhost because its a shorter url. And it works thanks.
happyCoding25
The local filesystem is better. The only reason to pull from a website is if you need it to be on a website (it has far higher overhead than a simple local file read)... And it's FAR less secure since it requires a TCP round trip (not much for localhost, but significantly more for any other domain due to DNS poisoning attacks and the like)... If you're going to be executing the results, read it from a local file as a rule...
ircmaxell
Brb, i'm in your hosts.conf.
A: 

Any particular reason you aren't just using popen?

http://www.php.net/manual/en/function.popen.php

Joshua D. Drake