0

I want to send a file to a remote server in PHP. When I make the file name hard-coded, the following code works fine:

$output = shell_exec('curl -XPOST -F "file=@myfile.txt" http://135.195.42.168:6007');
echo "<pre>$output</pre>";

Now I need to decide "file=@myfile.txt" dynamically. I tried the following:

$filePath = $_SERVER['DOCUMENT_ROOT'].'package/myzip_'.$owner_id.'.zip';
$name = basename($filePath);
$content = "file=@{$name}";
$output = shell_exec('curl -XPOST -F $content http://135.195.42.168:6007');
echo "<pre>$output</pre>";

Unfortunately, the above code does not work. Any better suggestion, please?

Martin
  • 117
  • 6

1 Answers1

1
$filePath = $_SERVER['DOCUMENT_ROOT'].'package/myzip_'.$owner_id.'.zip';
$name = basename($filePath);
$content = "file=@{$name}";
$output = shell_exec('curl -XPOST -F ' . $content . ' http://135.195.42.168:6007');
echo "<pre>$output</pre>";

should work fine

as well

$output = shell_exec("curl -XPOST -F $content 135.195.42.168:6007"); 

note:

' in php tells php to not interpret whatever is between '

$x = 'HELLO';


echo "what you say when you are nice? $x"; //outputs: what you say when you are nice? HELLO

this wouldn't work with ''

echo '\n'; //outputs \n

echo "\n"; //outputs a newline (not a
newline, but a carriage return)

generally I feel it's safer to use '' whenever possible. It's not only safer, it is generally more practical as well, because you can do

$output = shell_exec('curl -XPOST -F ' . str_replace('a', 'b', $content) . ' http://135.195.42.168:6007');

you don't have to, rereading your code, to scan all strings for maybe a hidden $

Toskan
  • 13,911
  • 14
  • 95
  • 185
  • Also, $output = shell_exec("curl -XPOST -F $content http://135.195.42.168:6007"); works fine. – Martin Apr 05 '18 at 16:20