简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM