How to use special characters inside php exec - php

Can anyone explain how to make this code work?
exec("ffmpeg -f concat -i <(printf "file '%s'\n" ./*.mp4) -c copy output.mp4");
I've tried to add '\' before " and ', it still didn't work.
Appreciate any help. Thanks!

You must escape double quotes and backslashes. You can do it using backslash: \
exec("ffmpeg -f concat -i <(printf \"file '%s'\\n\" ./*.mp4) -c copy output.mp4");

i know this question is super old but i had the same problem myself and couldn't find the answer anywhere
in case you are having this error on windows, to escape the special characters you have to use ^ instead of backslash
so stuff like
exec("some_application https://example.com/api.php?arg1=1&arg2=2")
won't work because of the & character, so it must become
exec("some_application https://example.com/api.php?arg1=1^&arg2=2")

Related

Is it possible to escape whitespaces from the filename/path string, using -F cURL for Android

I'm trying to run the following cURL/Android command:
-i -F uploadedfile=#/storage/emulated/0/Pictures/Corre y Se Va/IMG_20170719_223837_2031783171.jpg http://www.runonetworks.com/correyseva/upload_attachment.php?CODE=E49AC1D0
However, I'm getting this output:
curl: (26) couldn't open file "/storage/emulated/0/Pictures/Corre"
So It looks white spaces are causing trouble with my file path.
I've tried to replace white spaces with "/ " (Without quotes)
Also tried to have the whole string inside quotes ("#/storage...")
I can't seem to find a way to escape the whitespace character.
Does anyone know how?
I just had a look at the cURL man-page, found here: https://curl.haxx.se/docs/manpage.html#-F
Have you tried -i -F 'uploadedfile=#/storage/emulated/0/Pictures/Corre y Se Va/IMG_20170719_223837_2031783171.jpg' http://www.runonetworks.com/correyseva/upload_attachment.php?CODE=E49AC1D0? I've just wrapped the -F value in single quotes.
You,Should migrate path which incluide whitespace with '_' underscore in real path.then use underscored instead of white spaces.

Php and unix grep backslashing does not work

I have a fairly simple problem I just cannot seem to find a solution. I want to delete a line containing a certain string in all the files in a directory on my website. This string contains double quotes though. I have been trying to find a solution using backslashing but its does not work.
The string I want to replace is:session_register("c$key");
I'm using this php and unix code:
$command = "sed -i '/session_register(\"c$key\")/d;' ./*";
$output = shell_exec($command);
I want to delete this line because session_register is outdated.
It does not do anything but does not give me an error. What is the problem or how should I backslash?
Thanks a lot!
You need to double escape $ as well since $ is special regex symbol means end of input. Also use single quote instead of double for your $command string assignment.
Try this:
$command = 'sed -i.bak \'/session_register("c\\$key")/d;\' ./*';
$output = shell_exec($command);

phpseclib, using double slashes

OK so I have this line,
$ssh->exec('cd E:\\Titan\ Torque\\Jobs');
Now how to I use this with the double slashes? I mean I need 2 slashes to be sent not one the command:
cd E:\\Titan\ Torque\\Jobs
Is what I need to be executed.
P.S. The ssh server is running on windows, this command runs fine in putty but PHP is stripping it down to:
cd E:\Titan\ Torque\Jobs
Any help would be appreciated.
I think if you escape the backslashes with backslashes, they should work.
$ssh->exec('cd E:\\\\Titan\\ Torque\\\\Jobs');
In a test:
echo 'E\\:a thing with\slashes\\';
echo "\n";
echo 'E\\\\:a thing with\\\\slashes\\';
gives
E\:a thing with\slashes\
E\\:a thing with\\slashes\
In case you're curious, the reason a single backslash works at all, is because \ isn't a special escape sequence so it's put into the resultant string literally.

How to escape strings in for use in bash/sh via PHP's shell_exec?

I've been calling more advanced shell commands from PHP recently using shell_exec
As my commands become more complicated, I keep experiencing errors with things not being escaped properly. I want to be able to call shell_exec('echo '.$variable) and no matter what I put in $variable it will just echo it. Some things $variable could include are $ ~ ' " \n \r \c `` ( ) { } ; \
What's the best way to escape a shell command before executing it?
Does escapeshellcmd or escapeshellarg not do what you want?
shell_exec('echo '. escapeshellarg($variable));

Bash and PHP, quoting command line arguments

I have a PHP program that uses a Bash script to convert a pdf. However if the filename contains spaces it is not passed through the bash script correctly.
How do you escape filenames with spaces within a bash script? Do you have to do something special to quote the filename for the "OUTFILE" variable?
Bash script:
#!/bin/bash
INFILE=$1
OUTFILE=${INFILE%.*}
gs \
-q \
-dSAFER \
-dBATCH \
-dNOPAUSE \
-sDEVICE=png256 \
-r150x150 \
-sOutputFile=${OUTFILE}.png \
${INFILE}
PHP script:
echo "converting: ".$spool.$file . "\n";
system("/home/user/bin/pdf2png.sh " . escapeshellarg($spool . $file));
Edit: I removed the quotes around the escapeshellarg() variable. This however did not fix the problem. Which I think is in the Bash script OUTFILE variable.
In the last line of your shell script, put quotes around the variable reference:
"${INFILE}"
Considering your code, I would try by, first, removing the single-quotes you are inserting arround the parameter : those shouldn't be necessary, as you are using escapeshellarg.
For instance, the temp.php file might contain :
$spool = "ab cd/";
$file = "gh ij";
system("sh ./test.sh " . escapeshellarg($spool . $file) . "");
And the test.sh :
#!/bin/bash
INFILE=$1
echo $1
With those, the output is :
$ php temp.php
ab cd/gh ij
Which looks like what you expect.
If I put back the single-quotes, like this :
system("sh ./test.sh '" . escapeshellarg($spool . $file) . "'");
The output is broken again :
$ php temp.php
ab
escapeshellarg is escaping data for you (with the right quotes and all that, depending on the operating system), you don't have to do that yourself.
To escape a file name with spaces, just use .
My Tar Ball.tar
My\ Tar\ Ball.tar
So for what you have you will either need to make sure that your arguments contain the back slashes when the script is called, or you will need to add some input checking.
Take a look at escapeshellcmd() and escapeshellarg()

Categories