Harish Talanki
Harish Talanki

Reputation: 876

Escaping spaces in variable while passing as a command line argument in php

I have a php file say abc.php, this is processing some command line arguments and at the end calling another php say def.php,

system("php /user/release/scheduler_test/def.php $name $final > ~/scheduler_test/logs/logs_$name.txt 2>&1 &")

The problem here is, the variable $final is having a huge string separated with spaces and since for php the space is the delimiter, it is not taking the entire $final as one argument.

I want to pass the value inside this $final variable as a single value. Can anyone tell me how? I hope I am clear.

Upvotes: 3

Views: 1570

Answers (1)

That is why escapeshellarg is for. This does the escaping what you are exactly looking for.

A clearcut example from the PHP Docs..

<?php
system('ls '.escapeshellarg($dir));
?>

So escape your parameters/user-provided parameters using this function by wrapping this around it.

Upvotes: 4

Related Questions